XML DOM replaceData() 方法
Comment 對象
定義和用法
replaceData() 方法替換注釋節(jié)點中的數(shù)據(jù)。
語法
commentNode.replaceData(start,length,string)
參數(shù) | 描述 |
start | 必需。規(guī)定在何處替換字符。開始值以 0 開始。 |
length | 必需。規(guī)定替換多少個字符。 |
string | 必需。規(guī)定插入的字符串。 |
實例
下面的代碼片段使用 loadXMLDoc() 把 "books_comment.xml" 載入 xmlDoc 中,并把 "Simple" 替換成第一個 <book> 元素的注釋節(jié)點中的 "Easy":
實例
xmlDoc=loadXMLDoc("books_comment.xml");
x=xmlDoc.getElementsByTagName("book")[0].childNodes;
for (i=0;i<x.length;i++)
{
if (x[i].nodeType==8)
{
//Process only comment nodes
x[i].replaceData(4,6,"Easy");
document.write(x[i].data);
document.write("
");
}
}
輸出:
125 Easy and Delicious Recipes (Hardcover)
嘗試一下 ? 在上面的實例中,我們使用一段循環(huán)和 if 語句來執(zhí)行只針對 comment 節(jié)點的處理。comment 節(jié)點的節(jié)點類型是 8。
Comment 對象
更多建議: