return FALSE; $r = well_tag_thread__update(array('id' => $id), $update); return $r; } function well_tag_thread_find($tagid, $page, $pagesize) { $arr = well_tag_thread__find(array('tagid' => $tagid), array('id' => -1), $page, $pagesize); return $arr; } function well_tag_thread_find_by_tid($tid, $page, $pagesize) { $arr = well_tag_thread__find(array('tid' => $tid), array(), $page, $pagesize); return $arr; } ?>javascript in html head, innerhtml not working? - Stack Overflow
最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript in html head, innerhtml not working? - Stack Overflow

programmeradmin0浏览0评论
 <html>
 <head>
 <script type="text/javascript">
 document.getElementById("eee").innerHTML = "7777";
 </script>


 </head>
 <body>
 <p id="eee">aa</p>

 </body>
 </html>

why does the innerHTML not work in the head, but it does work in the body? Excuse the beginners question but last time i used javascript a year ago this was not a problem at all.

 <html>
 <head>
 <script type="text/javascript">
 document.getElementById("eee").innerHTML = "7777";
 </script>


 </head>
 <body>
 <p id="eee">aa</p>

 </body>
 </html>

why does the innerHTML not work in the head, but it does work in the body? Excuse the beginners question but last time i used javascript a year ago this was not a problem at all.

Share Improve this question asked Mar 24, 2012 at 4:41 Sam AdamshSam Adamsh 3,3919 gold badges34 silver badges54 bronze badges
Add a ment  | 

5 Answers 5

Reset to default 3

You need to wait for the HTML document to be loaded before it can be manipulated.

<script type="text/javascript">
  window.onload = function() {
    document.getElementById("eee").innerHTML = "7777";
  };
</script>

Keep in mind that scripts are evaluated in the order they are listed in an HTML document, so your script gets run while the browser is processing the "head" section. Since the browser hasn't yet parsed the "body" section it doesn't know about any element with id "eee".

However, by assigning a function to the "window.onload" event, you can delay the execution of your inner HTML assignment until the window has pletely loaded all of the resources and safely manipulate the document.

Note that if your script was in the body section, after the element with id "eee" was listed, then the script would work without the need to wait for the window "load" event.

You need to make sure the document is loaded.

Check answers to this question.

The javascript is running before the dom is ready. Use the event DOMContentLoaded in order to execute this in the header. For older browsers you should use the onload event.

http://jsfiddle/r7VYz/1/

 <html>
 <head>
 <script type="text/javascript">
 document.addEventListener('DOMContentLoaded', function () {
    document.getElementById("eee").innerHTML = "7777"; 
 }, false);

 </script>


 </head>
 <body>
 <p id="eee">aa</p>

 </body>
 </html>​

Its because you script cannot find the element. The scripts in the head load first, when the DOM has been loaded properly or still is loading.

<p id="eee">aa</p>
<!-- Use the script after the element, so that the script can find it -->
<script type="text/javascript">
 document.getElementById("eee").innerHTML = "7777";
</script>

Other methods may be executing the script on the onload events of the window

window.onload = function() {
    document.getElementById("eee").innerHTML = "7777";
};

You want something more like this. You have to wait for the entire document to load before you can be guaranteed to modify it. Like below

 <html>
<head>
 <script type="text/javascript">
    window.onload = function() {document.getElementById("eee").innerHTML = "7777";};
 </script>
 </head>
 <body>
 <p id="eee">aa</p>
 </body>
 </html>

Here is the working jsfiddle

发布评论

评论列表(0)

  1. 暂无评论