Thuộc tính độ dài DOM XML
❮ Đối tượng NamedNodeMap
Thí dụ
Đoạn mã sau tải " books.xml " vào xmlDoc và nhận số thuộc tính trong phần tử <book> đầu tiên:
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");
document.getElementById("demo").innerHTML =
"Number of
attributes in the first book element: " +
x.item(0).attributes.length;
}
Đầu ra của đoạn mã trên sẽ là:
Number of attributes in the first book element:
1
Định nghĩa và Cách sử dụng
Thuộc tính length trả về số lượng nút trong NamedNodeMap.
Cú pháp
nodemapObject.length
Bản trình diễn thử tự làm
❮ Đối tượng NamedNodeMap