Properti childNodes XML DOM
Definisi dan penggunaan
childNodes
Sifat mengembalikan NodeList bagi anak-nod yang dinyatakan.
Pemberitahuan:Anda boleh menggunakan sifat length untuk menentukan bilangan anak-nod, kemudian loping melintasi semua anak-nod dan mengambil maklumat yang andainginkan.
Sintaks
nodeObject.childNodes
Perincian teknikal
Hasil balik: | Objek NodeList yang mewakili kumpulan nod. |
---|---|
Versi DOM: | Core Level 1 |
Contoh
Contoh 1
Kod berikut akan memuat fail "books.xml" ke xmlDoc dan menunjukkan anak-n节 dokument 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 += "Nodename: " + x[i].nodeName + " (nodetype: " + x[i].nodeType + ")"; } document.getElementById("demo").innerHTML = txt; }
例子 2
显示 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, y, i, xmlDoc, txt; xmlDoc = xml.responseXML; txt = ""; x = xmlDoc.documentElement; y = x.childNodes; for(i = 0; i < y.length; i++) { txt += "Nodename: " + y[i].nodeName + " (nodetype: " + y[i].nodeType + ")<br>"; for(z = 0; z < y[i].childNodes.length; z++) { txt += "Nodename: " + y[i].childNodes[z].nodeName + " (nodetype: " + y[i].childNodes[z].nodeType + ")<br>"; } } document.getElementById("demo").innerHTML = "Nodename: " + xmlDoc.nodeName + " (nodetype: " + xmlDoc.nodeType + ")<br>" "Nodename: " + x.nodeName +" " (nodetype: " + x.nodeType + ")<br>" + txt; }
浏览器支持
Chrome | Edge | Firefox | Safari | Opera |
---|---|---|---|---|
Chrome | Edge | Firefox | Safari | Opera |
支持 | 支持 | 支持 | 支持 | 支持 |
所有主流浏览器都支持 childNodes
属性。