代码如下:
   <script>
function updoc()
         {
           document.writeln("aaa<br>");
           document.writeln("sss<br>");
           document.writeln("<script>");
           document.writeln("function updoc()");
           document.writeln("{");
           document.writeln("document.writeln('111<br>')");
           document.writeln("document.writeln('222<br>')");
           document.writeln("}");
           document.writeln("</scr"+"ipt>");
           document.writeln("<input type=button value=yes onclick=updoc()>");
         }
</script>
<input type=button value=yes onclick=updoc()>  为什么多次点击yes按钮后,写入的内容不会覆盖掉前面的内容呢?
  点击完按钮后,document对象不应该是不一样的么,求详细解释,谢谢~

解决方案 »

  1.   

    [code=HTML]<script>
    function updoc()
      {
      document.writeln("aaa<br>");
      document.writeln("sss<br>");
      document.writeln("<script>");
      document.writeln("function updoc()");
      document.writeln("{");
      document.writeln("document.writeln('111<br>')");
      /*加上close
      目前的情况看,应该是第二次按钮执行docuemnt.writeln的时候,浏览器没有自动关闭导致的。
      */
      document.writeln("document.writeln('222<br>');","document.close();");
      document.writeln("}");
      document.writeln("</scr"+"ipt>");
      document.writeln("<input type=button value=yddes onclick=updoc()>");   }
    </script>
    <input type=button value=yes onclick=updoc()>[/code]
      

  2.   

      尽量不要 用 document.writeln它会写在新窗口写入但是 页面加载js 时,又是在本窗口写入
      

  3.   

      不好意思各位,在下说的不够清楚。  其实我并不想实现什么,只是想知道这个问题出现的原因而已。  (转)如果document.write()方法在事件处理程序中调用,结果会覆盖当前文档,这因为JavaScript解析器会认为你是调用一个关闭的文档的write()方法,这样它就会隐式地打开一个新HTML文档,而当前文档被丢弃。   我想请问问的是为什么我前面写入的内容没有被后面写入的内容覆盖掉呢?   譬如,点击按钮后会先出现:
                                 aaa
                             sss
       再继续多次点按钮,会在下面出现:
                                     111
                                 222
                                 111
                                 222
                                ......
      为什么前面的aaa sss 没有被覆盖掉呢? 谢谢大家~
      
      

  4.   

    <script>
    function updoc()
    {
    document.writeln("aaa<br>");
    document.writeln("sss<br>");
    document.writeln("<script>");
    document.writeln("function updoc()");
    document.writeln("{");
    document.writeln("document.writeln('111<br>')");
    document.writeln("document.writeln('222<br>')");
    document.writeln("}");
    document.writeln("</scr"+"ipt>");
    document.writeln("<input type=button value=yes onclick=updoc()>");
    document.close();//关闭,否则就“继承”了
    }
    </script>
    <input type=button value=yes onclick=updoc()>
      

  5.   

    我没做过浏览器实现代码,不过我觉得可以这么理解。1.document.open()应该是在window对象中打开了一个内部的单例的输出文件流对象。2.document.write()或document.writeLn()方法先判断输出文件流对象是否已打开,未打开自动调用document.open()新建输出文件流对象,否则重用已打开的文件对象。3.函数updoc()退出,触发window解析输出文件流对象,重新生成document对象,不过window对象并不会自动关闭文件流。此时再次调用document.write()或document.writeLn(),仍使用未关闭文件流对象,相当于已经复制了现在的document内容,然后在后面追加新内容。 4.document.close()方法关闭文档,并迫使其内容显示出来。下次调用document.write()或document.writeLn()才会真正打开一个新文件流对象。
      

  6.   

    在你的代码中要执行:document.close();//方法关闭文档,并迫使其内容显示出来。
    这样才能使输出的内容显示出来!