$(document).bind('ready',function)和$(document).ready(function(){})之间有什么区别 [英] What are the difference between $(document).bind('ready', function) and $(document).ready(function() {})
问题描述
我想从requirejs版本2.0.0升级到2.1.5
I want to upgrade from requirejs version 2.0.0 to 2.1.5
以下是代码:
define(['jquery', 'test.js'],
function ($, test) {
var test = new $.test({
//options
});
....
});
test.js
(function($) {
var registerEvents = function() {
//dosth
};
$.test = function(options) {
$(document).bind('ready', function() {
registerEvents();
});
...
return test;
}
...
});
在版本2.0.0中,requirejs保存dom ready事件,直到所有资源都被下载,因此它工作正常正确 https://github.com/jrburke/requirejs/issues/249
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
当我升级到requirejs版本2.1.5时,将永远不会调用registerEvents函数。
When I upgrade to requirejs version 2.1.5, the registerEvents function will never be called.
但令人沮丧的是,如果我更改:
But supprisingly, if I change:
$(document).bind('ready', function() {
registerEvents();
});
收件人:
$(document).ready(function() {
registerEvents();
});
工作正常
所以我的问题是:它们之间有什么区别?
So my question is: What are the difference between them?
编辑:我使用的是jQuery v1.7.2
I am using jQuery v1.7.2
$(文件).on('ready',function(){})不工作
$(document).on('ready', function(){}) not working
推荐答案
区别在于,文档说
还有$(文档).on(ready,handler),自jQuery 1.8起不推荐使用。这与ready方法类似,但如果ready事件已经触发并且您尝试.on(ready)将不会执行绑定处理程序。就绪处理程序绑定这种方式是在上述其他三种方法绑定后执行的。 [em mine]
There is also $(document).on( "ready", handler ), deprecated as of 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 handlers bound this way are executed after any bound by the other three methods above. [em mine]
.bind
和 .on
行为相似。
这是唯一的区别
$( document ).ready( handler )
$().ready( handler ) // (this is not recommended)
$( handler )
和
$( 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
这篇关于$(document).bind('ready',function)和$(document).ready(function(){})之间有什么区别的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!