XML DOM getAttributeNode() 方法
定义和用法
getAttributeNode() 方法按名称从当前元素获取属性节点。
语法
elementNode.getAttributeNode(name)
| 参数 | 描述 |
|---|---|
| name | 必需。规定要获取的属性节点。 |
实例
下面的代码将 "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, attnode, xmlDoc, txt;
xmlDoc = xml.responseXML;
txt = "";
x = xmlDoc.getElementsByTagName('book');
for (i = 0; i < x.length; i++) {
attnode = x.item(i).getAttributeNode("category");
txt += attnode.name + " = " + attnode.value + "<br>";
}
document.getElementById("demo").innerHTML = txt;
}