XML DOM ตัวแปร localName

คำอธิบายและวิธีใช้

localName ตัวแปรกลับชื่อในท้องถิ่นขององค์ประกอบที่เลือก (ชื่อองค์ประกอบ)。

ถ้าจุดหมายที่เลือกไม่ใช่ตัวองค์ประกอบหรืออัตรายะสี ในตัวแปรนี้จะกลับค่า NULL。

ภาษา

elementNode.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;
}

親自試一試

ตัวอย่าง 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();
// ตรวจสอบชื่อเครื่องมือหลังสุดของจุดหมายหรือไม่ใช่ตัวองค์ประกอบ
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;
}

親自試一試