TreeWalker: nextSibling() 方法

Baseline 已廣泛支援

此特性已相當成熟,可在許多裝置和瀏覽器版本上使用。自 ⁨2015 年 7 月⁩以來,各瀏覽器均已提供此特性。

TreeWalker.nextSibling() 方法會將當前 Node 移動到其下一個同級節點(如果存在),並返回找到的同級節點。如果不存在這樣的節點,則返回 null,並且當前節點不會被更改。

語法

js
nextSibling()

引數

無。

返回值

一個 Node 物件或 null

示例

js
const treeWalker = document.createTreeWalker(
  document.body,
  NodeFilter.SHOW_ELEMENT,
  {
    acceptNode(node) {
      return NodeFilter.FILTER_ACCEPT;
    },
  },
  false,
);
treeWalker.firstChild();
const node = treeWalker.nextSibling(); // returns null if the first child of the root element has no sibling

規範

規範
DOM
# dom-treewalker-nextsibling

瀏覽器相容性

另見