Metodo XML DOM setAttributeNS()
❮ Oggetto Elemento
Esempio
Il seguente frammento di codice carica " book_ns.xml " in xmlDoc e aggiunge un attributo "edition" al primo elemento <book>:
var xhttp = new XMLHttpRequest();
xhttp.onreadystatechange = function() {
if (this.readyState == 4 && this.status == 200) {
myFunction(this);
}
};
xhttp.open("GET", "books_ns.xml", true);
xhttp.send();
function myFunction(xml) {
var xmlDoc = xml.responseXML;
var x =
xmlDoc.getElementsByTagName("book")[0];
var ns =
"https://www.w3schools.com/edition/";
x.setAttributeNS(ns,
"edition", "first");
document.getElementById("demo").innerHTML
=
x.getAttributeNS(ns,"edition");
}
Produzione:
first
Definizione e utilizzo
Il metodo setAttributeNS() aggiunge un nuovo attributo (con uno spazio dei nomi).
Se nell'elemento esiste già un attributo con quel nome o spazio dei nomi, il suo valore viene modificato in quello del prefisso e del parametro valore
Sintassi
elementNode.setAttributeNS(ns,name,value)
Parameter | Description |
---|---|
ns | Required. Specifies the namespace URI of the attribute to set |
name | Required. Specifies the name of the attribute to set |
value | Required. Specifies the value of the attribute to set |
Esempio
Il seguente frammento di codice carica " book_ns.xml " in xmlDoc e cambia il valore "lang" del primo elemento <title>:
var xhttp = new XMLHttpRequest();
xhttp.onreadystatechange = function() {
if (xhttp.readyState == 4 && xhttp.status == 200) {
myFunction(xhttp);
}
};
xhttp.open("GET", "books_ns.xml", true);
xhttp.send();
function myFunction(xml) {
var xmlDoc = xml.responseXML;
var x =
xmlDoc.getElementsByTagName("title")[0];
var ns =
"https://www.w3schools.com/edition/";
x.setAttributeNS(ns,
"c:lang", "italian");
document.getElementById("demo").innerHTML
=
x.getAttributeNS(ns, "lang");
}
Produzione:
italian
❮ Oggetto Elemento