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/childnodes/index.html | 52 ++++++++++++++++++++++++++ 1 file changed, 52 insertions(+) create mode 100644 files/zh-cn/web/api/node/childnodes/index.html (limited to 'files/zh-cn/web/api/node/childnodes') diff --git a/files/zh-cn/web/api/node/childnodes/index.html b/files/zh-cn/web/api/node/childnodes/index.html new file mode 100644 index 0000000000..182e6b177b --- /dev/null +++ b/files/zh-cn/web/api/node/childnodes/index.html @@ -0,0 +1,52 @@ +--- +title: Node.childNodes +slug: Web/API/Node/childNodes +tags: + - Gecko DOM Reference + - Property +translation_of: Web/API/Node/childNodes +--- +

{{ APIRef() }}

+

概述

+

Node.childNodes 返回包含指定节点的子节点的集合,该集合为即时更新的集合(live collection)。

+

语法

+
var ndList = elementNodeReference.childNodes;
+
+

ndList变量为 {{domxref("NodeList")}} 类型,且为只读。

+

例子

+
// parg 是一个到 <p> 元素的引用
+if (parg.hasChildNodes())
+// 首先我们检查它是否包含子节点
+ {
+   var children = parg.childNodes;
+   for (var i = 0; i < children.length; i++)
+   {
+   // children[i]就是遍历到的每个子节点.
+   // 注意:该NodeList对象为LIVE类型的NodeList, 添加或删除子节点都会实时的改变整个NodeList对象.
+   };
+ };
+
+
//下面的方法可以删除节点box的所有子节点
+while (box.firstChild)
+ {
+    //box为LIVE类型的NodeList,所以firstChild属性每次会指向不同的子节点
+    box.removeChild(box.firstChild);
+ };
+
+

备注

+

集合的元素是一个节点而不是字符串。要从集合的元素获取数据,你必须使用它们的属性(例如:用 elementNodeReference.childNodes{{ mediawiki.external("1") }}.nodeName 获取它们的名称,等等)。

+

document节点(文档节点)包含两个子节点: Doctype 声明和根节点。根节点通常为 documentElement 引用,且在 (X)HTML 文档中为 HTML 元素。

+

规范

+ +

相关链接

+ -- cgit v1.2.3-54-g00ecf