XML DOM childNodes 屬性
❮ 文件物件
示例
以下程式碼片段將 "books.xml" 載入到 xmlDoc 中,並顯示 XML 文件的子節點
var xhttp = new XMLHttpRequest();
xhttp.onreadystatechange = function() {
if (this.readyState == 4 && this.status == 200) {
myFunction(this);
}
};
xhttp.open("GET", "books.xml", true);
xhttp.send();
function myFunction(xml) {
var x, i, xmlDoc, txt;
xmlDoc = xml.responseXML;
txt = "";
x = xmlDoc.childNodes;
for (i = 0; i < x.length; i++) {
txt += "節點名: " + x[i].nodeName +
" (節點型別: " + x[i].nodeType + ")";
}
document.getElementById("demo").innerHTML = txt;
}
上面程式碼的輸出將是
節點名稱: bookstore (節點型別: 1)
IE9 及更早版本中的輸出
節點名: xml (節點型別: 7)
節點名稱: bookstore (節點型別: 1)
自己動手試一試 »
定義和用法
childNodes 屬性返回文件的子節點 NodeList。
語法
documentObject.childNodes
提示和註釋
提示: 使用 NodeList 的 length 屬性確定節點列表中的節點數。當您知道節點列表的長度時,可以輕鬆地遍歷它並提取所需的值!
❮ 文件物件