Element: nextElementSibling 属性

Baseline 已广泛支持

此特性已相当成熟,可在许多设备和浏览器版本上使用。自 ⁨2015 年 7 月⁩以来,各浏览器均已提供此特性。

Element.nextElementSibling 只读属性会返回其父级 children 列表中紧跟在指定元素之后的那个元素,如果指定元素是列表中的最后一个元素,则返回 null

一个 Element 对象,或者 null

示例

html
<div id="div-01">Here is div-01</div>
<div id="div-02">Here is div-02</div>
js
let el = document.getElementById("div-01").nextElementSibling;
console.log("Siblings of div-01:");
while (el) {
  console.log(el.nodeName);
  el = el.nextElementSibling;
}

当此示例加载时,它会在控制台输出以下内容:

Siblings of div-01:
DIV
SCRIPT

规范

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

浏览器兼容性

另见