English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية
nextSiblingThis property returns the next sibling node immediately following the element node (including text nodes, comment nodes such as carriage returns, line breaks, spaces, text, etc.).
If there is no next sibling, this property returnsnull.
Spaces are treated as text, and text is treated as a node. Comments are also treated as nodes.
To avoid the problem of nextSibling returning #text or #comment nodes, you can usenextElementSiblingOnly returns element nodes.
UsingpreviousSiblingproperty can return the previous node at the same tree level as the specified node.
UsingchildNodesThis property can return any child node of the specified node.
node.nextSibling
<div id="div-1">Here is div</>-1</div> <div id="div-2">Here is div</>-2</div> <script> var x = document.querySelector("#div-1).nextSibling.nodeName; document.getElementById("result").innerHTML = x; </script>Test and See‹/›
However, if the spaces between 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-1).nextSibling.innerHTML; document.getElementById("result").innerHTML = x; </script>Test and See‹/›
All browsers fully support the nextSibling property:
Property | |||||
nextSibling | Yes | Yes | Yes | Yes | Yes |
Return Value: | A Node object representing the next sibling of the node; if there is no next 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 .previousSibling property
HTML DOM Reference:node .nodeName property