最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - What are the difference between $(document).bind('ready', function) and $(document).ready(function(

programmeradmin4浏览0评论

I want to upgrade from requirejs version 2.0.0 to 2.1.5

Here is the code:

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;
    }

    ...
});

In version 2.0.0, requirejs holds the dom ready event till all resources are downloaded, so it worked correctly

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();
});

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

$(document).on('ready', function(){}) not working

I want to upgrade from requirejs version 2.0.0 to 2.1.5

Here is the code:

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;
    }

    ...
});

In version 2.0.0, requirejs holds the dom ready event till all resources are downloaded, so it worked correctly https://github./jrburke/requirejs/issues/249

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();
});

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

$(document).on('ready', function(){}) not working

Share Improve this question edited Dec 31, 2014 at 9:45 sharon asked Dec 31, 2014 at 9:27 sharonsharon 1351 silver badge7 bronze badges 13
  • 1 To everyone answering that there's no difference, can you explain why she got different results? Maybe requirejs treats them differently, even if jQuery itself doesn't. – Barmar Commented Dec 31, 2014 at 9:33
  • 2 @mangseth: bind is deprecated, not removed. – T.J. Crowder Commented Dec 31, 2014 at 9:35
  • 5 "I am using jQuery v1.7.2" Why?!?!?! – T.J. Crowder Commented Dec 31, 2014 at 9:35
  • 2 The difference is, as the docs say 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. – blgt Commented Dec 31, 2014 at 9:36
  • 3 As of jQuery 1.7, the .on() method is the preferred method for attaching event handlers to a document. For earlier versions, the .bind() method is used for attaching an event handler directly to elements. As I read it now, it's not deprecated, just not "preferred" then. – mnsth Commented Dec 31, 2014 at 9:39
 |  Show 8 more ments

1 Answer 1

Reset to default 8

The difference is, as the docs say

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 and .on behave similarly.


This is the only difference between

$( document ).ready( handler )
$().ready( handler ) // (this is not remended)
$( 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

与本文相关的文章

发布评论

评论列表(0)

  1. 暂无评论