Node.nextSibling read-only property returns the node immediately following the specified one in their parent's childNodes ,或返回 null if the specified node is the last child in the parent element.

句法

nextNode = node.nextSibling
					

注意事项

Gecko-based browsers insert text nodes into a document to represent whitespace in the source markup. Therefore a node obtained, for example, using Node.firstChild or Node.previousSibling may refer to a whitespace text node rather than the actual element the author intended to get.

Whitespace in the DOM and W3C DOM 3 FAQ: Why are some Text nodes empty? 了解更多信息。

Element.nextElementSibling may be used to obtain the next element skipping any whitespace nodes, other between-element text, or comments.

范例

<div id="div-1">Here is div-1</div>
<div id="div-2">Here is div-2</div>
<script>
var el = document.getElementById('div-1').nextSibling,
    i = 1;
console.group('Siblings of div-1:');
while (el) {
  console.log(i, '. ', el.nodeName);
  el = el.nextSibling;
  i++;
}
console.groupEnd();
</script>
/**************************************************
  The console displays the following:
     Siblings of div-1
      1. #text
      2. DIV
      3. #text
      4. SCRIPT
**************************************************/
					

In the above example, #text nodes are inserted in the DOM where whitespace occurs between tags (i.e. after the closing tag of an element and before the opening tag of the next).

The possible inclusion of text nodes must be allowed for when traversing the DOM using nextSibling . See the resources in the Notes section .

规范

规范 状态 注释
DOM
The definition of 'Node.nextSibling' in that specification.
实时标准 无变化
DOM (文档对象模型) 级别 2 核心规范
The definition of 'Node.nextSibling' in that specification.
过时 无变化
DOM (文档对象模型) 1 级规范
The definition of 'Node.nextSibling' in that specification.
过时 初始定义

浏览器兼容性

The compatibility table on this page is generated from structured data. If you'd like to contribute to the data, please check out https://github.com/mdn/browser-compat-data and send us a pull request. 更新 GitHub 上的兼容性数据
桌面 移动
Chrome Edge Firefox Internet Explorer Opera Safari Android webview Chrome for Android Firefox for Android Opera for Android Safari on iOS Samsung Internet
nextSibling Chrome 1 Edge 12 Firefox 1 IE 5.5 Opera 7 Safari 1.1 WebView Android 1 Chrome Android 18 Firefox Android 4 Opera Android 10.1 Safari iOS 1 Samsung Internet Android 1.0

图例

完整支持

完整支持

另请参阅

元数据

  • 最后修改: