HTML DOM Element nextSibling egenskab

Definition og brug

nextSibling Egenskaber returnerer næste node på samme træniveau.

nextSibling Returnerer er Node objekt.

nextSibling Egenskaber er skrivebeskyttede.

Bemærk

nextSibling Returnerer næste bror节点:elementknude, tekstknude eller kommentarknude.

Mellemrum mellem elementer er også tekstknuder.

Alternative solution:

nextElementSibling property - nextElementSibling Returns the next sibling element (ignores text and comment).

See also:

previousSibling property

firstChild property

lastChild property

childNodes property

hasChildNodes() method

HTML node and element

In HTML DOM(Document Object Model), an HTML document is a collection of nodes that have (or do not have) child nodes.

NodeRefers to element nodes, text nodes, and comment nodes.

ElementThe whitespace between them is also a text node.

And element is just an element node.

Sibling and element sibling

SiblingAre 'brother' and 'sister'.

SiblingAre nodes that have the same parent node (in the same childNodes in the list).

Element siblingAre elements that have the same parent element (in the same children in the list).

Child nodes and child elements

childNodes ReturnsChild node(Element node, text node, and comment node).

children ReturnsChild element(Not text and comment nodes).

nextSibling and nextElementSibling

nextSibling Returns the nextNode(Element node, text node, or comment node). The whitespace between elements is also a text node.

nextElementSibling Returns the nextElement(Does not return text and comment nodes).

previousSibling and previousElementSibling

previousSibling Returns the previousNode(Element node, text node, or comment node). The whitespace between elements is also a text node.

previousElementSibling Returns the previousElement(Does not return text and comment nodes).

Example

Returns the HTML content of the next sibling of the list item:

document.getElementById("item1").nextSibling.innerHTML;

Try it yourself

Syntax

element.nextSibling

or

node.nextSibling

Return value

Type Description
Node The next sibling of the element.
null If there is no next sibling.

Browser support

element.nextSibling It is a DOM Level 1 (1998) feature.

All browsers fully support it:

Chrome IE Edge Firefox Safari Opera
Chrome IE Edge Firefox Safari Opera
Support 9-11 Support Support Support Support