js中document.write使用过程中的一点疑问解答

  本人是新手,所以就记录下来了。这个答案是在百度中看到的,所以算是转载。

  下面的内容解答了何为如果在页面加载完后如果调用document.write就会覆盖整个文档。

  提示中的 【HTML 输出中】 指的是当页面加载的时候。

  

复制代码 代码如下:

  <html>

  <head></head>

  <body>

  <script type="text/javascript">document.write("<p>Hello</p>");</script>

  </body>

  </html>

  当页面加载完毕,你会在页面看到 Hello。查看源文件就是上面的代码。

  -------------------------

  但如果页面已经加载完毕,再使用 document.write,那就会覆盖整个文档。

  

复制代码 代码如下:

  <html>

  <head></head>

  <body>

  <script type="text/javascript">

  // 当点击鼠标时调用 document.write

  document.onclick = function() {

  document.write("<span>Javascript</span>");

  };

  </script>

  </body>

  </html>

  因为鼠标动作是在页面加载完毕后执行的,所以整个页面会被 <span>Javascript</span>所覆盖。现在查看源文件就只会看到 <span>Javascript</span>。