HTML DOM Element previousSibling egenskab

Definition og brug

previousSibling Egenskaben returnerer den foregående node på samme træniveau som Node objektet.

previousSibling Egenskaben er skrivebeskyttet.

Kommentar:Hvis der ikke er en previousSibling node, er returværdien null.

Bemærk

previousSibling Returnerer den foregående bror node: element node, tekst node eller kommentar node.

Mellemrum mellem elementer er også tekstknuder.

Alternativ løsning:

previousElementSibling egenskab - Returns the previous sibling element (ignores text and comment nodes).

See also:

nextSibling property

firstChild property

lastChild property

childNodes property

hasChildNodes() property

HTML node and element

In HTML DOM(Document Object Model), the 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 white space between them is also a text node.

While elements are just element nodes.

Sibling and element sibling

SiblingIs 'brother' and 'sister'.

SiblingIs a node that has the same parent node (in the same childNodes in the list).

Element siblingIs an element that has 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 white space 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 white space 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 previous sibling of the list item:

document.getElementById("item2").previousSibling.innerHTML;

Try it yourself

Syntax

element.previousSibling

or

node.previousSibling

Return value

Type Description
Node The previous sibling node of the node.
null If there is no preceding sibling.

Browser support

element.previousSibling 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