Jump to content

What Firstchild,nodevalue Doing Here?


dollar
 Share

Recommended Posts

<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>
Edited by dollar
Link to comment
Share on other sites

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

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

×
×
  • Create New...