1. Create a text node:
<script type= "Text/javascript" > function AddNode () { var element = document.createelement ("div"); Element.classname = "message"; var textnode = document.createTextNode ("Hello world!"); Element.appendchild (textnode); Document.body.appendChild (Element); } </script>
1.1 Occurrences of multiple text sub-nodes:
<script type= "Text/javascript" > function AddNode () { var element = document.createelement ("div"); Element.classname = "message"; var textnode = document.createTextNode ("Hello world!"); Element.appendchild (textnode); The other node var anothertextnode = document.createTextNode ("yippee!"); Element.appendchild (Anothertextnode); Insert into the body of the document Document.body.appendChild (element); } </script>
2. Normalized text node: normalize ()
<script type= "Text/javascript" > function AddNode () { var element = document.createelement ("div"); Element.classname = "message"; var textnode = document.createTextNode ("Hello world!"); Element.appendchild (textnode); Added another text node var anothertextnode = document.createTextNode ("yippee!"); Element.appendchild (Anothertextnode); Added to the BODY element of the document Document.body.appendChild (element); alert (element.childNodes.length); 2 <span style= "color: #ff0000;" >element.normalize ();//Normalization Process </span> alert (element.childNodes.length); 1 alert (element.firstChild.nodeValue); "Hello world! yippee! " } </script>
3. Split text node:
The effect is the opposite of the normalize () method: Splittext (). Divides a node into two, which splits the NodeValue value by the specified position.
<script type= "Text/javascript" > function AddNode () { var element = document.createelement ("div"); Element.classname = "message"; var textnode = document.createTextNode ("Hello world!"); Element.appendchild (textnode); Document.body.appendChild (element); <span style= "color: #ff0000;" > var newNode = element.firstChild.splitText (5);</span> alert (element.firstChild.nodeValue); "Hello" alert (newnode.nodevalue); "World!" alert (element.childNodes.length); 2 } </script>
Partition node is a common DOM parsing technique for extracting data from text nodes.
Text node of JavaScript advanced programming