DOMContentLoaded 事件是否与 jQuery 的 .ready() 函数完全相同? [英] Is DOMContentLoaded event EXACTLY the same as jQuery's .ready() function?

查看:24
本文介绍了DOMContentLoaded 事件是否与 jQuery 的 .ready() 函数完全相同?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经用 jQuery 的 $(document).bind('ready', function() {}) 替换了 window.addEventListener('DOMContentLoaded', function() {});;,因为第一个在 IE 上运行失败9 并且我不想为那个虚拟浏览器玩 .attachEvent() ,如果我可以很好地覆盖 jQuery 本身.

I have replaced window.addEventListener('DOMContentLoaded', function() {}); with jQuery's $(document).bind('ready', function() {});, because first one failed to work on IE < 9 and I did not wanted to play with .attachEvent() for that dummy browser, if I could have this nicely covered by jQuery itself.

替换后不久,我注意到 DOMContentLoaded 事件总是在页面加载/刷新后 0-2 毫秒左右触发(至少这是我的日志记录脚本记录的内容),而 .ready() 总是需要至少 15-20 毫秒,在页面刷新后被触发(再次 - 由脚本记录).

Shortly after replacement, I noticed that DOMContentLoaded event was always fired around 0-2 miliseconds after page load / refresh (at least this is what was logged by my logging script), while .ready() always requires at least 15-20 miliseconds, after page refresh, to be fired (again - as logged by script).

我纯粹是为了满足我的好奇心,为什么会有如此显着"的延迟?当然,对我来说没有问题,jQuery 稍后会触发该事件.只是,因为我想知道所有的答案(并统治世界!:]),我睡不着!:]

I'm asking purely for feeding my curiosity, why there is such "significant" delay? Of course, there is no problem for me, that jQuery is firing that event later. It is just, that because I want to know ALL the answers (and rule the world! :]), I can't sleep with that! :]

EDIT:在 .ready() 函数文档中一些用户(尼克(Nexxar)) 指出:jQuery 在 IE 上模拟了不存在的DOMContentLoaded"事件,但所使用的机制比其他浏览器上使用的事件触发得晚得多".也许这是一样的,我要的是?

EDIT: in .ready() function doc some user (Nick (of Nexxar)) points out that: "jQuery simulates the non existing "DOMContentLoaded" event on IE, but the used mechanism fires much later than the event used on other browsers". Maybe this is the same, I'm asking for?

推荐答案

假设浏览器支持该事件:

Assuming browser that supports the event:

  1. 真实事件可以支持任何document.jQuery 将只使用它加载的 document,无论你传递给它什么.
  2. 即使事件已经发生,jQuery 也会异步触发事件.如果事件已经发生,附加 'DOMContentLoaded' 事件将不起作用.
  1. The real event can support any document. jQuery will only use the document it was loaded in, no matter what you pass to it.
  2. jQuery will fire the event asynchronously even if the event has already happened. Attaching 'DOMContentLoaded' event will do nothing if the event has already happened.

这些浏览器没有延迟,参见 http://jsfiddle.net/rqTAX/3/(偏移量记录以毫秒为单位).

There is no delay in these browsers, see http://jsfiddle.net/rqTAX/3/ (the offsets logged are in milliseconds).

对于不支持该事件的浏览器,jQuery 显然也适用于它们.它将使用与真正的 DOMContentLoaded 不同的 hacky 机制,并且不一定会像真正的 DOMContentLoaded 那样立即触发:

For browsers that don't support the event, jQuery's will obviously work for them as well. It will use a hacky mechanism that is not the same as the real DOMContentLoaded and will not necessarily fire as soon as the real DOMContentLoaded would:

// The DOM ready check for Internet Explorer
function doScrollCheck() {
    if ( jQuery.isReady ) {
        return;
    }

    try {
        // If IE is used, use the trick by Diego Perini
        // http://javascript.nwbox.com/IEContentLoaded/
        document.documentElement.doScroll("left");
    } catch(e) {
        setTimeout( doScrollCheck, 1 );
        return;
    }

    // and execute any waiting functions
    jQuery.ready();
}

这篇关于DOMContentLoaded 事件是否与 jQuery 的 .ready() 函数完全相同?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆