简体   繁体   English

$(document).bind('ready',function)和$(document).ready(function(){})之间有什么区别

[英]What are the difference between $(document).bind('ready', function) and $(document).ready(function() {})

I want to upgrade from requirejs version 2.0.0 to 2.1.5 我想从requirejs版本2.0.0升级到2.1.5

Here is the code: 这是代码:

define(['jquery', 'test.js'],
    function ($, test) {
    var test = new $.test({
        //options
    });
    ....
});

test.js test.js

(function($) {
    var registerEvents = function() {
        //dosth
    };
    $.test = function(options) {
        $(document).bind('ready', function() {
            registerEvents();
        });
        ...
        return test;
    }

    ...
});

In version 2.0.0, requirejs holds the dom ready event till all resources are downloaded, so it worked correctly https://github.com/jrburke/requirejs/issues/249 在版本2.0.0中,requirejs保存dom ready事件,直到所有资源都被下载,因此它可以正常工作https://github.com/jrburke/requirejs/issues/249

When I upgrade to requirejs version 2.1.5, the registerEvents function will never be called. 当我升级到requirejs版本2.1.5时,将永远不会调用registerEvents函数。

But supprisingly, if I change: 但令人沮丧的是,如果我改变:

$(document).bind('ready', function() {
    registerEvents();
});

To: 至:

$(document).ready(function() {
    registerEvents();
});

It worked fine 它工作正常

So my question is: What are the difference between them? 所以我的问题是:它们之间有什么区别?

Edit: I am using jQuery v1.7.2 编辑:我正在使用jQuery v1.7.2

$(document).on('ready', function(){}) not working $(document).on('ready',function(){})不起作用

The difference is, as the docs say 正如文档所说 ,不同之处在于

There is also $(document).on( "ready", handler ), deprecated as of jQuery 1.8. 还有$(document).on(“ready”,handler),从jQuery 1.8开始不推荐使用。 This behaves similarly to the ready method but if the ready event has already fired and you try to .on( "ready" ) the bound handler will not be executed. 这与ready方法的行为类似,但如果ready事件已经触发并且您尝试.on(“ready”) ,则不会执行绑定处理程序。 Ready handlers bound this way are executed after any bound by the other three methods above. 以这种方式绑定的就绪处理程序在被上述其他三种方法绑定后执行。 [em mine] [我的]

.bind and .on behave similarly. .bind.on表现相似。


This is the only difference between 这是唯一的区别

$( document ).ready( handler )
$().ready( handler ) // (this is not recommended)
$( handler )

and

$( document ).on( "ready", handler )
$( document ).bind( "ready", handler )

that's mentioned in the docs, so I'm guessing is the most likely source of your issue 这是在文档中提到的,所以我猜你是最有可能的问题来源

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM