ویژگی attribute XML DOM
تعریف و استفاده
attribute
ویژگیها NamedNodeMap (لیست ویژگیها) را برمیگرداند که شامل ویژگیهای نود انتخاب شده است.
اگر نود انتخاب شده عناصر نباشد، این ویژگی NULL را برمیگرداند.
توجه:این ویژگی تنها برای نودهای عناصر کاربرد دارد.
قوانین
elementNode.attributes
مثال
کد زیر "books.xml" را به xmlDoc بارگذاری کرده و تعداد ویژگیهای اولین علامت <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" اولین علامت <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'); برای (i = 0; i < x.length; i++) { att = x.item(i).attributes.getNamedItem("category"); txt += att.value + "<br>"; } document.getElementById("demo").innerHTML = txt; }