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②

瀏覽器相容性

另見