XML DOM attribute attribute
ການອະທິບາຍ ແລະ ການນໍາໃຊ້
attribute
ບັນດາບັນຫານີ້ກັບ NamedNodeMap (ບັນດາບັນຫາ) ຂອງຫົວຂໍ້ບໍ່ສາມາດ.
ຖ້າຫົວຂໍ້ບໍ່ແມ່ນປະກອບສະຖານທີ່ ບັນດາບັນຫານີ້ຈະກັບຄືນ NULL.
ຄຳແນະນຳ:ບັນດາບັນຫານີ້ພຽງແຕ່ສະຖານທີ່ປະກອບ.
ວິທີການ
elementNode.attributes
ຄວາມນິຍົມ
ລະບັບດັ່ງລຽງຈະ "books.xml" ລ່າຍໄປ xmlDoc ແລະເອົາຈຳນວນຂອງ attribute ຂອງ ປະກອບ <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 ແລະເອົາຄວາມຂອງ ປະກອບ <book> ທຳອິດ "category" attribute: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; }