XML DOM childNodes 屬性

定義和用法
childNodes 屬性返回包含被選節點的子節點的 NodeList。
如果選定的節點沒有子節點,則該屬性返回不包含節點的 NodeList。
語法
elementNode.childNodes
提示和注釋
提示:如需迴圈遍曆 childNodes 列表,使用 nextSibling 屬性要比使用父對象的 childNodes 列表效率更高。
實例 1
下麵的代碼片段使用 loadXMLDoc() 把 "books.xml" 載入 xmlDoc 中,並從 "books.xml" 中第一個 <title> 元素取得文本節點:
實例
xmlDoc=loadXMLDoc("books.xml");
x=xmlDoc.getElementsByTagName("title")[0].childNodes[0];
document.write(x.nodeValue);
x=xmlDoc.getElementsByTagName("title")[0].childNodes[0];
document.write(x.nodeValue);
上面的代碼將輸出:
Everyday Italian
實例 2
下麵的代碼片段使用 loadXMLDoc() 把 "books.xml" 載入 xmlDoc 中,並從 "books.xml" 中第一個 <title> 元素取得子節點的數量:
實例
xmlDoc=loadXMLDoc("books.xml");
x=xmlDoc.getElementsByTagName("book")[0].childNodes;
document.write(x.length);
x=xmlDoc.getElementsByTagName("book")[0].childNodes;
document.write(x.length);
在 Internet Explorer 中,上面的代碼將輸出:
4
在 Mozilla 流覽器中,上面的代碼將輸出:
9
Firefox 以及大多數其他的流覽器,會把節點間生成的空的空格或者換行當作文本節點,而 Internet Explorer 會忽略節點間生成的空白文本節點。因此,在上面的實例中,輸出不一樣。
如需瞭解更多有關流覽器差異的知識,請在我們的 XML DOM 教學中訪問我們的 DOM 流覽器 章節。
