XML DOM localNameคุณสมบัติ
❮ วัตถุธาตุ
ตัวอย่าง 1
โค้ดย่อยต่อไปนี้โหลด " 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 xmlDoc = xml.responseXML;
var x = xmlDoc.getElementsByTagName("book")[0];
document.getElementById("demo").innerHTML =
x.localName;
}
ผลลัพธ์ของโค้ดด้านบนจะเป็น:
book
ความหมายและการใช้งาน
คุณสมบัติ localName ส่งกลับชื่อท้องถิ่น (ชื่อองค์ประกอบ) ขององค์ประกอบที่เลือก
หากโหนดที่เลือกไม่ใช่องค์ประกอบหรือแอตทริบิวต์ คุณสมบัตินี้จะคืนค่า NULL
ไวยากรณ์
elementNode.localName
ตัวอย่าง 2
โค้ดย่อยต่อไปนี้โหลด " books.xml " ลงใน xmlDoc และรับชื่อโลคัลจากโหนดย่อยสุดท้าย:
var xhttp = new XMLHttpRequest();
xhttp.onreadystatechange = function() {
if (this.readyState == 4 && this.status == 200) {
myFunction(this);
}
};
xhttp.open("GET", "books.xml", true);
xhttp.send();
//
Check if the last node is an element node
function get_lastchild(n) {
var x = n.lastChild;
while (x.nodeType != 1) {
x = x.previousSibling;
}
return
x;
}
function myFunction(xml) {
var xmlDoc = xml.responseXML;
var x = xmlDoc.documentElement;
var lastNode =
get_lastchild(x);
document.getElementById("demo").innerHTML
=
lastNode.localName;
}
ผลลัพธ์ของโค้ดด้านบนจะเป็น:
book
❮ วัตถุธาตุ