HTML DOM Element previousElementSibling 属性
- Previous Page previousSibling
- Next Page querySelector()
- Back to Top ເອັມເອມເອມເອມເລີມມອສນັນ
定义和用法
previousElementSibling
属性返回同一树级别中的前一个元素。
previousElementSibling
属性是只读的。
另请参阅:
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.
ElementWhitespace between elements is also a text node.
While element is just an element node.
Sibling vs 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 vs child element
childNodes ReturnsChild node(Element node, text node, and comment node).
children ReturnsChild element(Not text and comment nodes).
nextSibling vs nextElementSibling
nextSibling Returns the nextNode(Element node, text node, or comment node). Whitespace between elements is also a text node.
nextElementSibling Returns the nextElement(Does not return text and comment nodes).
previousSibling vs previousElementSibling
previousSibling Returns the previousNode(Element node, text node, or comment node). 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 previous sibling:
let text = element.previousElementSibling.innerHTML;
Syntax
element.previousElementSibling
Return value
Type | Description |
---|---|
Element | Previous sibling element. |
null | If there is no previous sibling. |
Browser support
element.previousElementSibling
Is DOM Level 3 (2004) 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 previousSibling
- Next Page querySelector()
- Back to Top ເອັມເອມເອມເອມເລີມມອສນັນ