XML DOM getNamedItem()เมธอด
❮ วัตถุ 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
ความหมายและการใช้งาน
เมธอด getNamedItem() ส่งคืนโหนดที่มีชื่อเฉพาะ (จาก namedNodeMap)
รองรับเบราว์เซอร์
เมธอด getNamedItem() ได้รับการสนับสนุนในเบราว์เซอร์หลักทั้งหมด
หมายเหตุ: Internet Explorer 8 และรุ่นก่อนหน้าไม่รองรับวิธีนี้
ไวยากรณ์
namedNodeMap.getNamedItem(nodeName)
พารามิเตอร์
Parameter | Type | Description |
---|---|---|
nodeName | String | Required. The name of the node to return |
คืนมูลค่า
Type | Description |
---|---|
Node object | The node with the specified name, , or null if it does not identify any node in the map |
รายละเอียดทางเทคนิค
เวอร์ชัน DOM | คอร์ระดับ 1 |
---|
ทดลองด้วยตัวเอง
❮ วัตถุ NamedNodeMap