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