addEventListener到窗口和文档对象。

时间:2014-01-27 03:32:54

标签: javascript

我希望有2个警报窗口。但我只有一个“窗口就绪”警报。为什么文档加载处理程序不工作?

window.addEventListener("load", function() { alert('window ready')});
document.addEventListener('load', function(){ alert('document ready')});

如果我将代码更改为

window.addEventListener("load",  alert('window ready'));
document.addEventListener('load',  alert('document ready'));

然后我看到2个警告框。我犯了任何愚蠢的错误或做了错误的假设吗?

非常感谢你。

1 个答案:

答案 0 :(得分:0)

jQuery.ready.promise = function( obj ) {
    if ( !readyList ) {

        readyList = jQuery.Deferred();

        // Catch cases where $(document).ready() is called after the browser event has already occurred.
        // we once tried to use readyState "interactive" here, but it caused issues like the one
        // discovered by ChrisS here: http://bugs.jquery.com/ticket/12282#comment:15
        if ( document.readyState === "complete" ) {
            // Handle it asynchronously to allow scripts the opportunity to delay ready
            setTimeout( jQuery.ready );

        } else {

            // Use the handy event callback
            document.addEventListener( "DOMContentLoaded", completed, false );

            // A fallback to window.onload, that will always work
            window.addEventListener( "load", completed, false );
        }
    }
    return readyList.promise( obj );
};

这是原始的jQuery代码。看起来文档对象上没有“就绪”事件名称。 “DOMContentLoaded”事件仅在HTML5中有效。

相关问题