XML DOM setAttributeNode()เมธอด
❮ วัตถุธาตุ
ตัวอย่าง
โค้ดย่อยต่อไปนี้โหลด " books.xml " ลงใน xmlDoc และเพิ่มแอตทริบิวต์ "edition" ให้กับองค์ประกอบ <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, y, z, i, newatt,
xmlDoc, txt;
xmlDoc = xml.responseXML;
txt = "";
x = xmlDoc.getElementsByTagName('book');
for (i = 0; i < x.length; i++) {
newatt = xmlDoc.createAttribute("edition");
newatt.value = "first";
x[i].setAttributeNode(newatt);
}
// Output all "edition" attribute values
for (i = 0; i < x.length; i++) {
txt += "Edition: " + x[i].getAttribute("edition") + "<br>";
}
document.getElementById("demo").innerHTML = txt;
}
เอาท์พุท:
Edition: first
Edition: first
Edition: first
Edition: first
ความหมายและการใช้งาน
setAttributeNode() วิธีการเพิ่มโหนดแอตทริบิวต์ใหม่
หากมีแอตทริบิวต์ที่มีชื่อนั้นอยู่แล้วในองค์ประกอบ แอตทริบิวต์นั้นจะถูกแทนที่ด้วยแอตทริบิวต์ใหม่ หากแอตทริบิวต์ใหม่แทนที่แอตทริบิวต์ที่มีอยู่ โหนดแอตทริบิวต์ที่ถูกแทนที่จะถูกส่งกลับ มิฉะนั้นจะส่งกลับค่าว่าง
ไวยากรณ์
elementNode.setAttributeNode(att_node)
Parameter | Description |
---|---|
att_node | Required. Specifies the attribute node to set |
❮ วัตถุธาตุ