<Oggetto Commento
Esempio
I seguenti frammento di codice carichi " books_comment.xml " in xmlDoc e ottiene il "(Hardcover) " stringa formano il primo commento elemento:
var xhttp = new XMLHttpRequest();
xhttp.onreadystatechange = function() {
if (xhttp.readyState == 4 && xhttp.status == 200) {
myFunction(xhttp);
}
};
xhttp.open("GET", "books_comment.xml", true);
xhttp.send();
function myFunction(xml) {
var x, i, y, txt,
xmlDoc;
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) {
y = x[i].substringData(33, 11);
txt += y + "<br>";
}
}
document.getElementById("demo").innerHTML = txt;
}
Produzione:
(Hardcover)
Prova tu stesso " Nel precedente esempio usiamo un ciclo e un se-test per assicurarsi che solo i processi di commento nodi. Un nodo commento ha un tipo di nodo di 8.
Definizione e utilizzo
Il substringData() metodo ottiene una stringa dal nodo commento.
Sintassi
substringData(start,length)
Parametro | Descrizione |
---|---|
start | Richiesto. Specifica dove cominciare caratteri estrazione. valore iniziale parte da zero |
length | Richiesto. Specifica il numero di caratteri da estrarre |
<Oggetto Commento