Méthode XML DOM appendData()
❮ Objet de commentaire
Exemple
Le fragment de code suivant charge « books_comment.xml » dans xmlDoc et ajoute du texte au premier élément de commentaire :
var xhttp = new XMLHttpRequest();
xhttp.onreadystatechange = function() {
if (this.readyState == 4 && this.status == 200) {
myFunction(this);
}
};
xhttp.open("GET",
"books_comment.xml", true);
xhttp.send();
function myFunction(xml) {
var x, i,
xmlDoc, txt;
xmlDoc = xml.responseXML;
txt = "";
x = xmlDoc.getElementsByTagName("book")[0].childNodes;
for (i = 0; i < x.length; i++) {
// Process only
comment nodes
if (x[i].nodeType
== 8) {
x[i].appendData(" Special Offer");
txt += x[i].data + "<br>";
}
}
document.getElementById("demo").innerHTML = txt;
}
Sortir:
125 Simple and Delicious Recipes (Hardcover) Special Offer
Dans l'exemple ci-dessus, nous utilisons une boucle et un test if pour nous assurer que nous ne traitons que les nœuds de commentaires. Un nœud de commentaire a un type de nœud de 8.
Définition et utilisation
La méthode appendData() ajoute des données à la fin d'un nœud de commentaire.
Syntaxe
commentNode.appedData(string)
Parameter | Description |
---|---|
string | Required. The string to add to the comment node |
❮ Objet de commentaire