XML DOM hasAttribute()メソッド
❮要素オブジェクト
例
T次のコードフラグメントは、「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");
}
上記のコードの出力は次のようになります。
true
定義と使用法
hasAttribute()メソッドは、現在の要素ノードに名前で指定された属性がある場合はTRUEを返し、それ以外の場合はFALSEを返します。
構文
hasAttribute(name)
Parameter | Description |
---|---|
name | Required. Specifies the attribute to find |
❮要素オブジェクト