XML DOM getAttribute() 方法
定义和用法
getAttribute()
方法通过名称获取属性值。
语法
elementNode.getAttribute(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, 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; }