Metodo XML DOM getAttribute()
❮ Oggetto Elemento
Esempio
Il seguente frammento di codice carica " books.xml " in xmlDoc e ottiene il valore dell'attributo "category" in 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, i, xmlDoc, txt;
xmlDoc = xml.responseXML;
txt = "";
x = xmlDoc.getElementsByTagName('book');
for (i = 0; i < x.length; i++) {
txt += x[i].getAttribute('category') + "<br>";
}
document.getElementById("demo").innerHTML = txt;
}
L'output del codice sopra sarà:
cooking
children
web
web
Definizione e utilizzo
Il metodo getAttribute() ottiene un valore di attributo in base al nome.
Sintassi
elementNode.getAttribute(name)
Parameter | Description |
---|---|
name | Required. Specifies the attribute to get the attribute value from |
❮ Oggetto Elemento