看来是不行,因为js执行时,页面还没装载完,找不到document.body,但能否在js中注册进这个事件监听呢?怎么写呢?

解决方案 »

  1.   

    应该用window.onload
    <script type="text/javascript">
    window.onload = function() {
    alert('Hello World');
    }
    </script>
      

  2.   

    楼上正确,比我的办法强,就一句话,我看了jquery的代码,解决了,不过长得多,贴出一点来:
    // Mozilla, Opera and webkit nightlies currently support this event
    if ( document.addEventListener ) {
    // Use the handy event callback
    document.addEventListener( "DOMContentLoaded", function(){
    document.removeEventListener( "DOMContentLoaded", arguments.callee, false );
    jQuery.ready();
    }, false ); // If IE event model is used
    } else if ( document.attachEvent ) {
    // ensure firing before onload,
    // maybe late but safe also for iframes
    document.attachEvent("onreadystatechange", function(){
    if ( document.readyState === "complete" ) {
    document.detachEvent( "onreadystatechange", arguments.callee );
    jQuery.ready();
    }
    });
    }