Visit Mozilla.org

DOM:element.childNodes

From MDC

« Gecko DOM 参考

目录

[编辑] 摘要

childNodes 返回包含当前元素的子元素的 集合.

[编辑] 语法和值

var ndList = elementNodeReference.childNodes; 

返回 ndList 集合,它包含当前元素已排序的子元素. 如果当前元素没有子元素, 那么 ndList 为空.

ndList 变量储蓄着子元素列表. 它是NodeList类型. childNodes 属性是只读的.

[编辑] 范例

// parg 是一个到 <p> 元素的引用
if (parg.hasChildNodes())
// 首先我们检查它是否包含子元素
 {
   var children = parg.childNodes;
   for (var i = 0; i < children.length; i++) 
   {
   // do something with each child as children[i]
   // NOTE: List is live, Adding or removing children will change the list
   };
 };
// This is one way to remove all children from a node
// box is an object refrence to an element with children
while (box.firstChild) 
 {
    //The list is LIVE so it will re-index each call
    box.removeChild(box.firstChild);
 };

[编辑] 注意

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

document 对象包含两个子对象: Doctype declaration 和HTML 元素.

[编辑] 规范