วิธี XML DOM getNamedItem()
คำนำออกและการใช้งาน
getNamedItem()
วิธีที่มีชื่อเฉพาะของโครงสร้าง (จาก namedNodeMap) ถูกส่งกลับโดยวิธี
ภาษา
namedNodeMap.getNamedItem(ข้อความ)}}
ประกาศ
ประกาศ | คำอธิบาย |
---|---|
ข้อความ | จำเป็น |
รายละเอียดเทคโนโลยี
เวอร์ชั่น DOM | Core Level 1 |
---|---|
ค่าที่กลับคืน |
Node ตัวแทน ถ้าไม่มีหน้าตาหรือตัวแทนในการแปลงภาษานี้ ก็จะกลับค่า null |
ตัวอย่าง
รหัสด้านล่างนี้จะนำ "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 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; }
ตัวอย่าง 2
เปลี่ยนค่าของคุณสมบัติ
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, attlist, att, xmlDoc, txt; xmlDoc = xml.responseXML; txt = ""; x = xmlDoc.getElementsByTagName("book"); // เปลี่ยนค่าของคุณสมบัติ attribute ของ category for (i = 0; i < x.length; i++) { attlist = x.item(i).attributes; att = attlist.getNamedItem("category"); att.value = "BESTSELLER"; } // ออกสัญญาณทั้งหมด title และ edition for (i = 0; i < x.length; i++) { txt += x[i].getAttribute("category") + "<br>"; } document.getElementById("demo").innerHTML = txt; }
瀏覽器支持
Chrome | Edge | Firefox | Safari | Opera |
---|---|---|---|---|
Chrome | Edge | Firefox | Safari | Opera |
支持 | 支持 | 支持 | 支持 | 支持 |
所有主流瀏覽器都支持 getNamedItem()
方法。
註釋:Internet Explorer 8 及更早版本不支持此方法。