HTML DOM Element 属性 previousSibling
- Previous Page parentElement
- Next Page previousElementSibling
- Go Up One Level HTML DOM Elements 对象
定义和用法
previousSibling
属性返回同一树级别的上一个节点,以 Node 对象。
previousSibling
属性是只读的。
注释:如果没有 previousSibling 节点,则返回值是 null
。
注意
previousSibling
返回上一个同胞节点:元素节点、文本节点或注释节点。
元素之间的空白也是文本节点。
替代方案:
属性 previousElementSibling - Returns the previous sibling element (ignores text and comment nodes).
See Also:
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 blank 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 Node and Child Element
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 blank 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 blank 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 list item:
document.getElementById("item2").previousSibling.innerHTML;
Syntax
element.previousSibling
or
node.previousSibling
Return Value
Type | Description |
---|---|
Node | The previous sibling node of the node. |
null | If there is no previous 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 |
- Previous Page parentElement
- Next Page previousElementSibling
- Go Up One Level HTML DOM Elements 对象