XML DOM คุณสมบัติ length

คำนิยามและวิธีใช้

length คุณสมบัติที่กลับค่าจำนวนของโครงสร้างข้อมูลในลิสต์ของตัวประกอบ.

รูปแบบ

nodelistObject.length

ตัวอย่าง

ตัวอย่าง 1

รหัสต่อไปนี้จะโหลด "books.xml" ลงใน xmlDoc และหาจำนวนขององค์ประกอบ <title>:

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("title");
    document.getElementById("demo").innerHTML =
    "จำนวนองค์ประกอบของหัวข้อ: " + x.length;
}

ทดลองด้วยตัวเอง

ตัวอย่าง 2

ลูกฟังก์ชันวนลูกฟังก์ชันผ่านจุดมาตราฐานของโครงสร้างข้อมูล:

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('title');
    for (i = 0 ; i <x.length; i++) {
        txt += x[i].childNodes[0].nodeValue + "<br>";
    }
    document.getElementById("demo").innerHTML = txt;
}

ทดลองด้วยตัวเอง