English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية
previousSiblingA read-only property returns the previous node at the same tree level as the specified node.
If there is no previous peer, this property returnsnull.
spaces are treated as text, text is treated as a node. Comments are also treated as nodes.
To avoid the problem of previousSibling returning #text or #comment nodes, you can usepreviousElementSiblingIt only returns element nodes.
UsenextSiblingproperty to return the next node at the same tree level as the specified node.
UsechildNodesThis property can return any child node of the specified node.
node.previousSibling
<div id="div-1">Here is div</>-1</div> <div id="div-2">Here is div</>-2</div> <script> var x = document.querySelector("#div-2).previousSibling.nodeName; document.getElementById("result").innerHTML = x; </script>Test See‹/›
However, if the spaces between the DIVs are removed, the returned value will not be #text:
<div id="div-1">Here is div</>-1</div><div id="div-2">Here is div</>-2</div> <script> var x = document.querySelector("#div-2).previousSibling.innerHTML; document.getElementById("result").innerHTML = x; </script>Test See‹/›
All browsers fully support the previousSibling property:
Property | |||||
previousSibling | Yes | Yes | Yes | Yes | Yes |
Return Value: | A Node object representing the previous sibling of the node; if there is no previous sibling, it isnull |
---|---|
DOM Version: | DOM Level1 |
HTML DOM Reference:node.childNodes property
HTML DOM Reference:node.firstChild property
HTML DOM Reference:node.lastChild property
HTML DOM Reference:node.parentNode property
HTML DOM Reference:node.nextSibling property
HTML DOM Reference:node.nodeName property