dom - javascript why "document.documentElement.childNodes" output different result? -


I think "document.documentElement.cildNodes" are already all children in the tag, but today I have my code extrovert I'm looking for a special case:

    

Why would I get "document.documentElement.childNodes" in tags and function tags, different results? I hope someone can give me more examples about this. Thanks a lot!

The issue is, do this in a head script tag when it is executed, to the whole DOM The page is not loaded, however, and when you call the function in the console, the DOM is fully loaded, to make sure that all your code is window .onload to the event, like this:

  window.addEventListener ("load", function () {var o = document.createElement ( 'Script'); o.text = 'alert ("111")'; Var Ohtml = document.documentElement; Warning (ohtml.nodeName); // Output HTML Warning (ohtml.childNodes.length); // nodes are not length 1 alert (ohtml.childNodes.length); // just head is not hobbled hair Node [0] .appendChild (o);});   

And if you do not want to use window.onload , then put it in your body tag:

  & lt; Body & gt; & Lt ;! - Your content - & gt; & Lt; Script & gt; Warning (ohtml.nodeName); // Output HTML Alert (ohtml.childNodes.length); // nodes are not length 1 alert (ohtml.childNodes.length); // just head & lt; / Script & gt; & Lt; / Body & gt;    

Comments

Popular posts from this blog

c - Mpirun hangs when mpi send and recieve is put in a loop -

python - Apply coupon to a customer's subscription based on non-stripe related actions on the site -

java - Unable to get JDBC connection in Spring application to MySQL -