Thuộc tính length của XML DOM
Định nghĩa và cách sử dụng
length
Thuộc tính trả về số lượng phần tử trong danh sách nút.
Cú pháp
nodelistObject.length
Mô hình
例子 1
Dưới đây là mã để tải "books.xml" vào xmlDoc và lấy số lượng phần tử <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 = "Số lượng phần tử tiêu đề: " + 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; }