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