XML DOM hasAttribute() 方法
定義和用法
如果當前元素節點擁有指定名稱的屬性,則 hasAttribute()
方法返回 true,否則返回 false。
語法
hasAttribute(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 xmlDoc = xml.responseXML; var x = xmlDoc.getElementsByTagName("book")[0]; document.getElementById("demo").innerHTML = x.hasAttribute("category"); }