XML DOM setAttributeNS() Metode
❮ Elementvoorwerp
Voorbeeld
Die volgende kodefragment laai " books_ns.xml " in xmlDoc en voeg 'n "edition"-kenmerk by die eerste <book>-element:
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");
}
Uitset:
first
Definisie en gebruik
Die setAttributeNS() metode voeg 'n nuwe kenmerk by (met 'n naamspasie).
As 'n kenmerk met daardie naam of naamruimte reeds in die element bestaan, word die waarde daarvan verander om dié van die voorvoegsel en waardeparameter te wees
Sintaksis
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 |
Voorbeeld
Die volgende kode fragment laai " books_ns.xml " in xmlDoc en verander die "lang" waarde van die eerste <title> element:
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");
}
Uitset:
italian
❮ Elementvoorwerp