XML DOM hasAttribute()เมธอด
❮ วัตถุธาตุ
ตัวอย่าง
TThe โค้ดย่อยต่อไปนี้โหลด " 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 |
❮ วัตถุธาตุ