XML DOM replaceData() メソッド

定義と使用法

replaceData() テキストノード内のデータを置き換えるメソッド。

構文

replaceData(start,length,string)
パラメータ 説明
start 必須。置き換えを開始する場所を指定します。ゼロから始まる値です。
length 必須。置き換える文字数を指定します。
string 必須。挿入する文字列を指定します。

以下のコードは "books.xml" を xmlDoc に読み込み、最初の <title> 要素のテキストノードの前八字を "Easy" に置き換えます:

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 xmlDoc = xml.responseXML;
    var x = xmlDoc.getElementsByTagName("title")[0].childNodes[0];
    document.getElementById("demo").innerHTML =
    x.nodeValue;
    x.replaceData(0,8, "Easy");
    document.getElementById("demo").innerHTML +=
    "<br>" + x.nodeValue;
}

自分で試してみる