Metodo XML DOM setAttributeNode()
❮ Oggetto Elemento
Esempio
Il seguente frammento di codice carica " books.xml " in xmlDoc e aggiunge un attributo "edition" a tutti gli elementi <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;
}
Produzione:
Edition: first
Edition: first
Edition: first
Edition: first
Definizione e utilizzo
Il metodo setAttributeNode() aggiunge un nuovo nodo attributo.
Se un attributo con quel nome esiste già nell'elemento, viene sostituito da quello nuovo. Se il nuovo attributo sostituisce un attributo esistente, viene restituito il nodo dell'attributo sostituito, altrimenti restituisce null.
Sintassi
elementNode.setAttributeNode(att_node)
Parameter | Description |
---|---|
att_node | Required. Specifies the attribute node to set |
❮ Oggetto Elemento