XML DOM length attribute

การระบุและการใช้งาน

length attribute คืนค่าความยาวของข้อความในตัวอย่างหมายเหตุ (ด้วยตัวอักษร)。

ภาษา

commentNode.length

ตัวอย่าง

รหัสที่แสดงด้านล่างจะ "books_comment.xml" โหลดเข้าไปใน xmlDoc และจาก element <title> แรกของเนื้อหาของตัวอย่างและความยาวของเนื้อหา:

var xhttp = new XMLHttpRequest();
xhttp.onreadystatechange = function() {
   if (this.readyState == 4 && this.status == 200) {
       myFunction(this);
   }
};
xhttp.open("GET", "books_comment.xml", true);
xhttp.send();
function myFunction(xml) {
    var x, i, xmlDoc, txt;
    xmlDoc = xml.responseXML;
    txt = "";
    x = xmlDoc.getElementsByTagName("book")[0].childNodes;
    for (i = 0; i < x.length; i++) {
    // รับงานเพียงตัวอย่างหมายเหตุ
        if (x[i].nodeType == 8) {
            txt += x[i].length + "<br>";
        }
    }
    document.getElementById("demo").innerHTML = txt;
}

親自試一試

在上面的例子中,我們用了迴圈和 if 測試語句,來確保我們只處理注釋節點。注釋節點的節點類型為 8。