Metodo insertData() del DOM XML
Definizione e uso
insertData()
Il metodo inserisce dati nel nodo di commento.
Sintassi
commentNode.insertData(start,string)
Parametro | Descrizione |
---|---|
start | Obbligatorio. Specifica da dove iniziare ad inserire i caratteri. Il valore di partenza è zero. |
string | Obbligatorio. Specifica la stringa da inserire. |
Esempio
Il codice seguente carica "books_comment.xml" nella xmlDoc e inserisce la stringa nel primo nodo di commento:
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++) { // Solo i nodi di commento vengono trattati if (x[i].nodeType == 8) { x[i].insertData(25, "Italian "); txt += x[i].data + "<br>"; } } document.getElementById("demo").innerHTML = txt; }
在上面的例子中,我们使用了循环和 if 测试语句,以确保我们只处理注释节点。注释节点的节点类型为 8。