XML DOM setAttribute() Metode
❮ Elementvoorwerp
Voorbeeld
Die volgende kodefragment laai " books.xml " in xmlDoc en voeg 'n "edition"-kenmerk by alle <boek>-elemente:
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, xmlDoc, txt;
xmlDoc = xml.responseXML;
txt = "";
x = xmlDoc.getElementsByTagName('title');
// Add a new
attribute to each title element
for (i = 0; i <
x.length; i++) {
x[i].setAttribute("edition",
"first");
}
// Output titles
and edition value
for (i = 0; i < x.length; i++) {
txt += x[i].childNodes[0].nodeValue +
" - Edition: " +
x[i].getAttribute('edition')
+ "<br>";
}
document.getElementById("demo").innerHTML = txt;
}
Uitset:
Everyday Italian - Edition: first
Harry Potter - Edition: first
XQuery Kick Start - Edition: first
Learning XML - Edition: first
Definisie en gebruik
Die setAttribute() metode voeg 'n nuwe kenmerk by.
As 'n kenmerk met daardie naam reeds in die element bestaan, word die waarde daarvan verander om dié van die waardeparameter te wees
Sintaksis
elementNode.setAttribute(name,value)
Parameter | Description |
---|---|
name | Required. Specifies the name of the attribute to set |
value | Required. Specifies the value of the attribute to set |
Probeer-dit-self-demo's
❮ Elementvoorwerp