XML DOM insertData() メソッド
定義と使用法
insertData()
コメントノードにデータを挿入するメソッドです。
文法
commentNode.insertData(start,string)
パラメータ | 説明 |
---|---|
start | 必須。挿入を開始する場所を指定します。値は0から始まります。 |
string | 必須。挿入する文字列を指定します。 |
例
以下のコードは "books_comment.xml" を xmlDoc に読み込み、文字列を挿入する最初のコメントノードに:
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++) { // 只处理注释节点 if (x[i].nodeType == 8) { x[i].insertData(25, "Italian "); txt += x[i].data + "<br>"; } } document.getElementById("demo").innerHTML = txt; }
上記の例では、コメントノードのみを処理するようにループと if テストを使用しました。コメントノードのノードタイプは 8 です。