Proprietà XML DOM textContent
❮ Oggetto nodo
Esempio
Il seguente frammento di codice carica " books.xml " in xmlDoc e restituisce il contenuto testuale degli 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.item(i).textContent
+ "<br>";
}
document.getElementById("demo").innerHTML = txt;
}
L'output del codice sopra sarà:
Everyday Italian Giada De Laurentiis 2005 30.00
Harry Potter J K. Rowling 2005 29.99
XQuery Kick Start James McGovern Per Bothner Kurt Cagle James Linn
Vaidyanathan Nagarajan 2003 49.99
Learning XML Erik T. Ray 2003 39.95
Definizione e utilizzo
La proprietà textContent imposta o restituisce il contenuto testuale di un nodo e dei suoi discendenti.
Al momento dell'impostazione, tutti i nodi figlio vengono rimossi e sostituiti da un singolo nodo di testo contenente la stringa su cui è impostata questa proprietà.
Sintassi
nodeObject.textContent
Dimostrazioni fai-da-te
❮ Oggetto nodo