dollar Posted April 4, 2009 Share Posted April 4, 2009 <html><body><p id="intro">W3Schools example</p><div id="main"><p id="main1">The DOM is very useful</p><p id="main2">This example demonstrates how to use the <b>nodeValue</b> property</p></div><script type="text/javascript">x=document.getElementById("intro").firstChild;document.write("First paragraph text: " + x.nodeValue);</script></body></html>What that firstChild, nodeValue will do there? What they will tell to browser?======BTW, what's the difference between below two codes? I've noticed they both do same thing...<html><body><p id="intro">W3Schools example</p><div id="main"><p id="main1">The DOM is very useful</p><p id="main2">This example demonstrates how to use the <b>nodeValue</b> property</p></div><script type="text/javascript">x=document.getElementById("intro").firstChild;document.write("First paragraph text: " + x.nodeValue);</script></body></html> <html><body><p id="intro">W3Schools example</p><div id="main"><p id="main1">The DOM is very useful</p><p id="main2">This example demonstrates how to use the <b>nodeValue</b> property</p></div><script type="text/javascript">x=document.getElementById("intro").innerHTML;document.write("First paragraph text: " + x);</script></body></html> Link to comment Share on other sites More sharing options...
justsomeguy Posted April 6, 2009 Share Posted April 6, 2009 document.getElementById("intro") refers to this node:<p id="intro">W3Schools example</p>document.getElementById("intro").firstChild refers to the text node that contains the text "W3Schools example". document.getElementById("intro").firstChild.nodeValue refers to the actual text value.The 2 pieces of code show the same thing because, in that case, document.getElementById("intro").firstChild.nodeValue and document.getElementById("intro").innerHTML refer to the same thing (the text in the <p> element). Link to comment Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.