XML DOM replaceData() Method
Definition and Usage
The replaceData() method uses the specified string to replace the data in the comment node.
Syntax:
commentNode.replaceData(start,length,string)
parameter | description |
---|---|
start | is required. Specifies where to replace the character. This value starts from 0. |
length | is required. The number of characters to be replaced. |
string | is required. The number of characters to be replaced by start and length The string specified by the character string. |
description
This method uses a string string replacement from start starting length characters. If start add length is greater than the length of the Comment node, then from start All characters starting will be replaced.
Example
This code segment uses the JavaScript function loadXMLDoc() Ladda XML-filen books_comment.xml Ladda xmlDoc, och använd "Paperback" för att ersätta den första <book>-elementets kommentar nodens "Hardcover":
xmlDoc=loadXMLDoc("books_comment.xml");
x=xmlDoc.getElementsByTagName("book")[0].childNodes;
for (i=0;i<x.length;i++)
{
if (x[i].nodeType==8)
{
//Bara behandla comment noder
x[i].replaceData(10,9,"Easy");
document.write(x[i].data);
document.write("<br />");
}
}
Uppgiften för ovanstående kod är:
(Bok 6) (Pappersbok)
I detta exempel använder vi en loop och en if-sats för att utföra behandling endast för comment noder. comment nodernas nodtyp är 8.
Relaterade sidor
XML DOM referenshandbok:CharacterData.replaceData()