From 33058f2b292b3a581333bdfb21b8f671898c5060 Mon Sep 17 00:00:00 2001 From: Peter Bengtsson Date: Tue, 8 Dec 2020 14:40:17 -0500 Subject: initial commit --- files/zh-cn/web/api/node/nextsibling/index.html | 78 +++++++++++++++++++++++++ 1 file changed, 78 insertions(+) create mode 100644 files/zh-cn/web/api/node/nextsibling/index.html (limited to 'files/zh-cn/web/api/node/nextsibling') diff --git a/files/zh-cn/web/api/node/nextsibling/index.html b/files/zh-cn/web/api/node/nextsibling/index.html new file mode 100644 index 0000000000..2ea2eb4e2b --- /dev/null +++ b/files/zh-cn/web/api/node/nextsibling/index.html @@ -0,0 +1,78 @@ +--- +title: Node.nextSibling +slug: Web/API/Node/nextSibling +translation_of: Web/API/Node/nextSibling +--- +
{{APIRef}}
+ +

Node.nextSibling 是一个只读属性,返回其父节点的 {{domxref("Node.childNodes","childNodes")}} 列表中紧跟在其后面的节点,如果指定的节点为最后一个节点,则返回 null

+ +

语法

+ +
nextNode = node.nextSibling
+
+ +

备注

+ +

 

+ +

Gecko内核的浏览器会在源代码中标签内部有空白符的地方插入一个文本结点到文档中.因此,使用诸如 Node.firstChildNode.previousSibling 之类的方法可能会引用到一个空白符文本节点, 而不是使用者所预期得到的节点.

+ +

详情请参见 DOM 中的空白符W3C DOM 3 FAQ: 为什么一些文本节点是空的.

+ +

 

+ +

示例

+ +
<div id="div-01">Here is div-01</div>
+<div id="div-02">Here is div-02</div>
+
+<script type="text/javascript">
+var el = document.getElementById('div-01').nextSibling,
+    i = 1;
+
+console.log('Siblings of div-01:');
+
+while (el) {
+  console.log(i + '. ' + el.nodeName);
+  el = el.nextSibling;
+  i++;
+}
+
+</script>
+
+/**************************************************
+  The following is written to the console as it loads:
+
+     Siblings of div-01
+
+      1. #text
+      2. DIV
+      3. #text
+      4. SCRIPT
+
+**************************************************/
+
+ +

从上面的例子中可以看出,在两个标签之间(即一个元素的闭合标签之后,下一个元素的起始标签之前)有空白出现时,会有#text 节点被插入到 DOM 中。使用 document.write 语句插入的两个元素之间不会有空白。

+ +

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

+ +

规范

+ + + +

相关链接

+ + + +

浏览器兼容性

+ + + +

{{Compat("api.Node.nextSibling")}}

-- cgit v1.2.3-54-g00ecf