Difference between .on('click') vs .click()

后端 未结 12 2501
礼貌的吻别
礼貌的吻别 2020-11-21 05:22

Is there any difference between the following code?

$(\'#whatever\').on(\'click\', function() {
     /* your code here */
});

and

相关标签:
12条回答
  • 2020-11-21 05:33
    $('.UPDATE').click(function(){ }); **V/S**    
    $(document).on('click','.UPDATE',function(){ });
    
    $(document).on('click','.UPDATE',function(){ });
    
    1. it is more effective then $('.UPDATE').click(function(){ });
    2. it can use less memory and work for dynamically added elements.
    3. some time dynamic fetched data with edit and delete button not generate JQuery event at time of EDIT OR DELETE DATA of row data in form, that time $(document).on('click','.UPDATE',function(){ }); is effectively work like same fetched data by using jquery. button not working at time of update or delete:

    0 讨论(0)
  • 2020-11-21 05:34

    Here you will get list of diffrent ways of applying the click event. You can select accordingly as suaitable or if your click is not working just try an alternative out of these.

    $('.clickHere').click(function(){ 
         // this is flat click. this event will be attatched 
         //to element if element is available in 
         //dom at the time when JS loaded. 
    
      // do your stuff
    });
    
    $('.clickHere').on('click', function(){ 
        // same as first one
    
        // do your stuff
    })
    
    $(document).on('click', '.clickHere', function(){
              // this is diffrent type 
              //  of click. The click will be registered on document when JS 
              //  loaded and will delegate to the '.clickHere ' element. This is 
              //  called event delegation 
       // do your stuff
    });
    
    $('body').on('click', '.clickHere', function(){
       // This is same as 3rd 
       // point. Here we used body instead of document/
    
       // do your stuff
    });
    
    $('.clickHere').off().on('click', function(){ // 
        // deregister event listener if any and register the event again. This 
        // prevents the duplicate event resistration on same element. 
        // do your stuff
    })
    
    0 讨论(0)
  • 2020-11-21 05:37

    .on() is the recommended way to do all your event binding as of jQuery 1.7. It rolls all the functionality of both .bind() and .live() into one function that alters behavior as you pass it different parameters.

    As you have written your example, there is no difference between the two. Both bind a handler to the click event of #whatever. on() offers additional flexibility in allowing you to delegate events fired by children of #whatever to a single handler function, if you choose.

    // Bind to all links inside #whatever, even new ones created later.
    $('#whatever').on('click', 'a', function() { ... });
    
    0 讨论(0)
  • 2020-11-21 05:37

    No, there isn't.
    The point of on() is its other overloads, and the ability to handle events that don't have shortcut methods.

    0 讨论(0)
  • 2020-11-21 05:38

    Is there any difference between the following code?

    No, there is no functional difference between the two code samples in your question. .click(fn) is a "shortcut method" for .on("click", fn). From the documentation for .on():

    There are shorthand methods for some events such as .click() that can be used to attach or trigger event handlers. For a complete list of shorthand methods, see the events category.

    Note that .on() differs from .click() in that it has the ability to create delegated event handlers by passing a selector parameter, whereas .click() does not. When .on() is called without a selector parameter, it behaves exactly the same as .click(). If you want event delegation, use .on().

    0 讨论(0)
  • 2020-11-21 05:38

    As mentioned by the other answers:

    $("#whatever").click(function(){ });
    // is just a shortcut for
    $("#whatever").on("click", function(){ })
    

    Noting though that .on() supports several other parameter combinations that .click() doesn't, allowing it to handle event delegation (superceding .delegate() and .live()).

    (And obviously there are other similar shortcut methods for "keyup", "focus", etc.)

    The reason I'm posting an extra answer is to mention what happens if you call .click() with no parameters:

    $("#whatever").click();
    // is a shortcut for
    $("#whatever").trigger("click");
    

    Noting that if you use .trigger() directly you can also pass extra parameters or a jQuery event object, which you can't do with .click().

    I also wanted to mention that if you look at the jQuery source code (in jquery-1.7.1.js) you'll see that internally the .click() (or .keyup(), etc.) function will actually call .on() or .trigger(). Obviously this means you can be assured that they really do have the same result, but it also means that using .click() has a tiny bit more overhead - not anything to worry or even think about in most circumstances, but theoretically it might matter in extraordinary circumstances.

    EDIT: Finally, note that .on() allows you to bind several events to the same function in one line, e.g.:

    $("#whatever").on("click keypress focus", function(){});
    
    0 讨论(0)
提交回复
热议问题