XML DOM attribute คุณสมบัติ
คำอธิบายและวิธีใช้
attribute
คุณสมบัติกลับค่า NamedNodeMap (ลิสต์ของคุณสมบัติ) ที่มีคุณสมบัติของตัวเลือกที่เลือก
ถ้าตัวเลือกที่เลือกไม่ใช่ element คุณสมบัตินี้จะกลับค่า NULL
คำเตือน:คุณสมบัตินี้มีผลบนตัวเลือกของ element แค่
ภาษาเขียน
elementNode.attributes
ตัวอย่าง
ข้อความต่อไปนี้จะนำ "books.xml" ใส่ xmlDoc และเรียกใช้จำนวนของอัตรายะใน element <title> แรกของ "books.xml"
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].attributes; document.getElementById("demo").innerHTML = x.length; }
ตัวอย่าง
2 ข้อความต่อไปนี้จะนำ "books.xml" ใส่ xmlDoc และเรียกใช้ค่าของอัตรายะตัวแปร "category" ใน element <book> แรก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, att, xmlDoc, txt; xmlDoc = xml.responseXML; txt = ""; x = xmlDoc.getElementsByTagName('book'); for (i = 0; i < x.length; i++) { att = x.item(i).attributes.getNamedItem("category"); txt += att.value + "<br>"; } document.getElementById("demo").innerHTML = txt; }