用main.document.write();实现时候
背景颜色没了,colorit()函数也只执行了一次!
那么也就是说document.write()会清空当前对象的代码?

解决方案 »

  1.   

    document.write是在当前浏览器页面里输出,原来的代码不复存在,也就是说循环执行一次就结束了。
      

  2.   

    执行后看下浏览器的HTML源码。
      

  3.   

    document.write 在页面加载完毕后调用就会清空当前的页面
      

  4.   

    看看官方的解释: You should not use the write or writeln methods on the current document after the document has finished loading unless you first call the open method, which clears the current document's window and erases all variables
      

  5.   

    果然如此...!
    初玩JavaScript 见笑了 还望以后大家多多指点!
      

  6.   

    should not use the write or writeln methods on the current document after the document has finished loading
      

  7.   

    呵呵 我又对代码优化了一次
    原代码使用了循环输出文字 这样的效率很明显的降低了
    所以我把循环去掉了o(∩_∩)o...<head>
    <title>JavaScript Test</title>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
    <style>
    body{background:#000000; color:#FFFFFF; font-family:Georgia;}
    </style>
    <script language="javascript" type="text/javascript">
    <!--
    var str="Http://wWw.Csdn.Net";
    var color="red";
    speed=100;
    var i=0;
    var outstr="";
    document.write("<div id=main align=center></div>");
    function doit()
    {
      setInterval("redit()",speed)
    }
    function redit()
    {
       outstr=str.substring(0,i);
       outstr+=str.charAt(i).fontcolor(color);
       outstr+=(str.substring(i+1,str.length));
       main.innerHTML=outstr;
       outstr="";
       (i < str.length)?i++:i=0;
    }
    -->
    </script>
    </head>
    <body onLoad="doit()">
    </body>