这个好像是没有办法了,因为innerHTML中的字符串显示在窗口中时的过程并不是js引擎来处理而是HTML解析器(姑且这样认为,可能不叫HTML解析器,但是反正不是js引擎) 干的活,从这个意义上说当js引擎将这段字符串传给了HTML解析器后就认为这个语句已经执行完毕,马上就执行下面的alert语句,但是实际上HTML解析器显示出来是需要时间的。
所以这个动作就好像是两个进程在同时工作,就看哪个执行快了。笨些的方法就是让alert延时。

解决方案 »

  1.   

    本地测试,setTimeout();
    <script language="javascript" type="text/javascript">

    function t()
    {
    window.setTimeout('modual()', 2000);
    } function modual()
    {
    document.getElementById('t').innerHTML = "<input type='hidden' id='flag' value='flag'><h3 style='color:green;'>Hello world!</h3><input type='hidden' id='flag' value='flag'><h3 style='color:green;'>Hello world!</h3><input type='hidden' id='flag' value='flag'><h3 style='color:green;'>Hello world!</h3><input type='hidden' id='flag' value='flag'><h3 style='color:green;'>Hello world!</h3><input type='hidden' id='flag' value='flag'><h3 style='color:green;'>Hello world!</h3><input type='hidden' id='flag' value='flag'><h3 style='color:green;'>Hello world!</h3><input type='hidden' id='flag' value='flag'><h3 style='color:green;'>Hello world!</h3><input type='hidden' id='flag' value='flag'><h3 style='color:green;'>Hello world!</h3><input type='hidden' id='flag' value='flag'><h3 style='color:green;'>Hello world!</h3><input type='hidden' id='flag' value='flag'><h3 style='color:green;'>Hello world!</h3><input type='hidden' id='flag' value='flag'><h3 style='color:green;'>Hello world!</h3>";
    alert('OK');
    }</script>
    </HEAD><BODY>
    <input type="button" value="Go!" onclick="t();">
    <span id='t'></span>
    </BODY>
    </HTML>
      

  2.   

    我觉得可以在str的最后一行加上一个回调函数,这样说不定能够达到效果,不过我没没试过。还有一种办法,如果插入的那些html是一个元素的话,可以给这个元素加一个onload事件,这样也可行