CharacterData:nextElementSibling 属性

Baseline 已广泛支持

此功能已成熟,可跨多种设备和浏览器版本工作。它自 ⁨2018 年 4 月⁩ 起已在所有浏览器中可用。

CharacterData 接口的只读 nextElementSibling 属性返回指定节点在父节点子节点列表中紧随其后的第一个 Element 节点,如果指定节点是列表中的最后一个节点,则返回 null

一个 Element 对象,如果没有找到同胞节点,则为 null

示例

html
TEXT
<div id="div-01">Here is div-01</div>
TEXT2
<div id="div-02">Here is div-02</div>
<pre>Here is the result area</pre>
js
// Initially, set node to the Text node with `TEXT`
let node = document.getElementById("div-01").previousSibling;

let result = "Next element siblings of TEXT:\n";

while (node) {
  result += `${node.nodeName}\n`;
  node = node.nextElementSibling; // The first node is a CharacterData, the others Element objects
}

document.querySelector("pre").textContent = result;

规范

规范
DOM
# ref-for-dom-nondocumenttypechildnode-nextelementsibling②

浏览器兼容性

另见