XML DOM getAttribute() 方法
❮ 元素对象
示例
以下代码片段将 "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, xmlDoc, txt;
xmlDoc = xml.responseXML;
txt = "";
x = xmlDoc.getElementsByTagName('book');
for (i = 0; i < x.length; i++) {
txt += x[i].getAttribute('category') + "<br>";
}
document.getElementById("demo").innerHTML = txt;
}
上面代码的输出将是
cooking
children
网页
网页
自己动手试一试 »
定义和用法
getAttribute() 方法通过名称获取属性值。
语法
elementNode.getAttribute(name)
参数 | 描述 |
---|---|
name | 必需。指定要从中获取属性值的属性 |
❮ 元素对象