javascript

liuhuiyang119 2010-02-04 05:15:56
在javascript中使用定时器调用函数,函数里有document.write("hello");将会出错,请问,是什么原因 ???


因为你把页面包括脚本全部清空了??

這个解释能详细点吗?谢谢
...全文
98 6 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
6 条回复
切换为时间正序
请发表友善的回复…
发表回复
wangshuainihao 2011-04-27
  • 打赏
  • 举报
回复
document.write();
用定时器调用时,会清空!!
浴火_凤凰 2010-02-04
  • 打赏
  • 举报
回复
最简单明了的回答:你运行一下程序,出错后,然后查看一下源文件就明白了。
brakeout 2010-02-04
  • 打赏
  • 举报
回复
document.write()调用之后会把之前的html清空,然后再输出大括号里面的内容。
草根醉秋意 2010-02-04
  • 打赏
  • 举报
回复
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.
草根醉秋意 2010-02-04
  • 打赏
  • 举报
回复
在文档完全加载完了以后调用document.write函数会重写原来的文档,也就是把原来文档的内容全部清空而只有document.write方法输出的内容。因此应该避免在文档完全加载了以后再调用document.write方法。

当然在文档完全加载之前是可以使用的,那样就会在文档调用这个方法的地方输出内容。
bart1988 2010-02-04
  • 打赏
  • 举报
回复
你在JAVASCRIPT里面定时器调用了某个函数。而这个函数的里有document.write,这个document.write会把原来页面内容清空。定时器在间隔时间来执行的时候,会找不到这个方法了。就会报错。

87,997

社区成员

发帖
与我相关
我的任务
社区描述
Web 开发 JavaScript
社区管理员
  • JavaScript
  • 无·法
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧