ویژگی 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; }