Text nodes contain plain text that is interpreted literally. The text may contain escaped HTML characters but no HTML tags. A text node has the following characteristics
- nodeType is 3
- nodeName is “#text”
- nodeValue and data contain the text in the node
- parentNode is an Element node
- child nodes are not supported
- length indicates the number of text characters in nodeValue
Any place that may have text has a text node, including places where whitespace exists.
We can create text nodes using the createTextNode() method and append the node to an element node with appendChild().
var textNode = document.createTextNode("hello"); pinkBox.appendChild(textNode);
The text in a text node can be modified in any of the following ways:
- The nodeValue and data properties may be set directly
- appendData(text) appends text to the end of the node
- deleteData(offset, count) deletes count number of characters starting at offset
- insertData(offset, text) inserts text at offset
- replaceData(offset, count, text) replaces content starting at offset through offset + count with text
- splitText(offset) splits the text node into two text nodes
- substringData(offset, count) extracts a substring of length count from the text starting at offset
© 2018 – 2020, Eric. All rights reserved.