รายการ XML DOM ()เมธอด
❮ วัตถุ NamedNodeMap
ตัวอย่าง
โค้ดย่อยต่อไปนี้โหลด " books.xml " ลงใน xmlDoc วนซ้ำผ่านองค์ประกอบ <book> และพิมพ์ค่าของแอตทริบิวต์หมวดหมู่:
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, att, xmlDoc,
txt;
xmlDoc = xml.responseXML;
txt = "";
x = xmlDoc.getElementsByTagName('book');
for (i = 0; i < x.length; i++) {
att = x.item(i).attributes.getNamedItem("category");
txt += att.value + "<br>";
}
document.getElementById("demo").innerHTML = txt;
}
ผลลัพธ์ของโค้ดด้านบนจะเป็น:
cooking
children
web
web
ความหมายและการใช้งาน
รายการ () วิธีการส่งคืนโหนดที่ดัชนีที่ระบุในรายการโหนด
ไวยากรณ์
item(index)
Parameter | Description |
---|---|
index | The index |
ทดลองด้วยตัวเอง
❮ วัตถุ NamedNodeMap