网络学院 w3pop社区 网络资源 IT新闻

w3pop.com :: 网络学院 :: XML DOM :: childNodes 属性

会员登陆

帐号

密码

回答

记住密码

忘记密码? 注册

childNodes 属性


作者:w3pop.com 翻译/整理:w3pop.com 发布:2007-04-29 浏览:1133 :: ::

Definition and Usage
定义和用法

The childNodes property returns a NodeList containing the child nodes of the selected node
childNodes属性的作用是:返回一个已选节点的子节点的节点列表。

If the selected node has no children, this property returns a NodeList containing no nodes.
如果以选择的节点不包含子节点,那么这个属性返回部包含任何节点的节点列表。

Syntax
语法

elementNode.childNodes
 

Tips and Notes
提示

Tip: To loop over a childNodes list, it is more efficient to use the nextSibling property than to explicitly use the childNodes list of the parent object.
提示:如果你希望循环显示一个节点列表,那么使用nextSibling属性比简单的使用parent对象的子节点列表更加有效。


In all examples, we will use the XML file books.xml, and the JavaScript function loadXMLDoc().
在所有案例中,我们将使用“book.xml”文件以及JavaScript 函数“loadXMLDoc()”。

Example 1
案例1

The following code fragment prints the text node from the first <title> element in "books.xml":
下面的代码片断将输出“books.xml”文件的第一个<title>元素:

xmlDoc=loadXMLDoc("books.xml");
var x=xmlDoc.getElementsByTagName("title")[0].childNodes[0]
document.write(x.nodeValue);

The output of the code above will be:
上述代码将输出下面的结果:

Everyday Italian


Example 2
案例2

The following code fragment prints the number of child nodes from the first <book> element in "books.xml":
下面的代码片断将输出“books.xml”文件的第一个<book>元素的子节点数量:

xmlDoc=loadXMLDoc("books.xml");
var x=xmlDoc.getElementsByTagName("book")[0].childNodes;
document.write(x.length);

In Internet Explorer the output of the code above will be:
上述代码将输出下面的结果:

4

In Mozilla browsers the output of the code above will be:
在Mozilla 浏览器中输出的结果如下:

9

Internet Explorer will skip white-space text nodes that are generated between nodes (e.g. new-line characters), while Mozilla will not. So, in the example above, the output will be different.
IE将会跳过在节点中产生的空格文本节点(如:换行符),然而Mozilla不会。因此,上述案例在两种浏览器中输出的结果是不同的。

To read more about the differences between IE and Mozilla browers, visit our Mozilla vs. IE chapter in our XML DOM Tutorial.
如果你希望知道更多关于IE和Mozilla浏览器之间的不同,你可以访问XML DOM教程中的“Mozilla vs IE”这章。


Try-It-Yourself Demos
自我演示

getelementsByTagname() and childNodes - Get an element's value
getelementsByTagname() 和 childNodes - 获取元素值

childNodes - Get the NodeList's length
childNodes - 获取元素列表长度

评论 (0) All