在javascript中使用定时器调用函数,函数里有document.write("hello");将会出错,请问,是什么原因 ???
因为你把页面包括脚本全部清空了??這个解释能详细点吗?谢谢

解决方案 »

  1.   

    你在JAVASCRIPT里面定时器调用了某个函数。而这个函数的里有document.write,这个document.write会把原来页面内容清空。定时器在间隔时间来执行的时候,会找不到这个方法了。就会报错。
      

  2.   

    在文档完全加载完了以后调用document.write函数会重写原来的文档,也就是把原来文档的内容全部清空而只有document.write方法输出的内容。因此应该避免在文档完全加载了以后再调用document.write方法。当然在文档完全加载之前是可以使用的,那样就会在文档调用这个方法的地方输出内容。
      

  3.   

    http://javascript.about.com/library/blwrite.htm Any document.write statement that runs after the page finishes loading will create a new page and overwrite all of the content of the current page. This is almost certainly not what you intend to have happen. You should therefore avoid using document.write in situations such as this:
    <script type="text/javascript">
    function w1() {
    document.write('hello world'); // overwrite entire page
    }
    window.onload = w1;
    </script>So you can only use document.write at best to write the original content of your page. It cannot be used to update the content of your page after that page has loaded. 
      

  4.   

    document.write()调用之后会把之前的html清空,然后再输出大括号里面的内容。
      

  5.   

    document.write();
    用定时器调用时,会清空!!