Entry
how to use the "TextNode" Object's method ,for example,insertdata,appenddata
Apr 3rd, 2001 07:03
Mike Hall, feiy hy,
Here's a simple example you can experiment with:
<p id="test">
This is a sentence.
</p>
<a href=""
onclick="document.getElementById('test').firstChild.appendData('
Appended text.');return false">Append</a>
|
<a href=""
onclick="document.getElementById('test').firstChild.insertData(5, '
inserted text');return false">Insert</a>
|
<a href=""
onclick="document.getElementById('test').firstChild.deleteData(5,
14);return false">Delete</a>
|
<a href=""
onclick="document.getElementById('test').firstChild.replaceData(6, 8,
'replacement ');return false">Replace</a>
Note that document.getElementById('test') gives the node for the P
element. Applying .firstChild to that gives the text node for the string
"This is a sentence."
.appendData(str) adds a string to the end of the text node's current value.
.insertData(offset, str) inserts a string into the current text node
string at the given offset.
.deleteData(offset, n) removes n characters from the node string
starting at offset.
.replaceData(offset, n, str) acts like deleteData() but then inserts the
given string at the offset.
Note that IE does not support these methods - as of 5.5 at least, anyone
try IE6? But you can achieve the same effects using regular
JavaScript String methods on the text node's .nodeValue property. For
example:
document.getElementById('test').firstChild.nodeValue += ' Appended text.'