jQuery: $().click(fn) vs. $().bind('click',fn);

前端 未结 7 1742
北荒
北荒 2020-11-28 03:37

When using jQuery to hookup an event handler, is there any difference between using the click method

$().click(fn)

versus using the bind me

相关标签:
7条回答
  • 2020-11-28 04:08

    +1 for Matthew's answer, but I thought I should mention that you can also bind more than one event handler in one go using bind

    $('#myDiv').bind('mouseover focus', function() {
        $(this).addClass('focus')
    });
    

    which is the much cleaner equivalent to:

    var myFunc = function() {
        $(this).addClass('focus');
    };
    $('#myDiv')
        .mouseover(myFunc)
        .focus(myFunc)
    ;
    
    0 讨论(0)
提交回复
热议问题