Node text in JavaScript
Let's now figure out how to read or change the text
of the nodes. For all nodes, the textContent
property works. For text nodes and comments, there
are properties nodeValue and data
(they are almost the same, the second one is shorter,
it's better to use it). For elements there is a
property innerHTML that reads the text
along with the tags.
Given a div:
<div id="elem">txt<b>tag</b><!--com-->txt<b>tag</b><!--com--></div>
Loop through all the nodes of this div and output the texts of all nodes to the console.
Given a div:
<div id="elem">txt<b>tag</b><!--com-->txt<b>tag</b><!--com--></div>
Loop through all nodes of this div and print to the console the texts of all comments and text nodes.
Given a div:
<div id="elem">txt<b>tag</b><!--com-->txt<b>tag</b><!--com--></div>
Loop through all the nodes of this div and output to the console the texts of the text nodes and elements.
Given a div:
<div id="elem">txt<b>tag</b><!--com-->txt<b>tag</b><!--com--></div>
Loop through all the nodes of this div and write down the type of each node to the its end.