XML DOM attribute 属性
定义和用法
attribute 属性返回 NamedNodeMap(属性列表),其中包含所选节点的属性。
如果所选节点不是元素,则此属性返回 NULL。
提示:此属性仅适用于元素节点。
语法
elementNode.attributes
实例
下面的代码将 "books.xml" 加载到 xmlDoc 中,并获取 "books.xml" 中第一个 <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("book")[0].attributes;
document.getElementById("demo").innerHTML =
x.length;
}
实例
2 下面的代码将 "books.xml" 加载到 xmlDoc 中,并获取第一个 <book> 元素中 "category" 属性的值:
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, att, xmlDoc, txt;
xmlDoc = xml.responseXML;
txt = "";
x = xmlDoc.getElementsByTagName('book');
for (i = 0; i < x.length; i++) {
att = x.item(i).attributes.getNamedItem("category");
txt += att.value + "<br>";
}
document.getElementById("demo").innerHTML = txt;
}