Propriété textContent de XML DOM
Définition et utilisation
textContent
L'attribut définit ou retourne le contenu texte du noeud et de ses descendants.
Lorsque l'attribut est défini, tous les noeuds enfants sont supprimés et remplacés par un seul noeud texte contenant cette valeur d'attribut.
Syntaxe
nodeObject.textContent
Exemple
Exemple 1
Le code suivant charge "books.xml" dans xmlDoc et retourne le contenu texte des éléments <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; }
Exemple 2
Définir le contenu texte du noeud :
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'); // Définir textContent for(i = 0; i < x.length; i++) { x.item(i).textContent = "Outdated"; } // Afficher textContent for(i = 0; i < x.length; i++) { txt += x.item(i).textContent + "<br>"; } document.getElementById("demo").innerHTML = txt; }