How to overwrite jquery event handlers

后端 未结 6 2009
逝去的感伤
逝去的感伤 2020-12-13 01:31

I\'ll try to explain the problem with a simple code.

var fireClick = function() { alert(\'Wuala!!!\') };

$(\'#clickme\').click(fireclick);
$(\'#clickme\').c         


        
6条回答
  •  有刺的猬
    2020-12-13 02:16

    As of jQuery 1.7 you should be using off to remove event handlers and on to add them, though you can still use the click shorthand.

    $('#clickme').off('click').on('click', fireclick);
    $('#clickme').off().on('click', fireclick);
    

    Original answer:

    If you want to replace all click handlers, call unbind first without a function argument. If you want to replace all event handlers, don't specify the event type.

    $('#clickme').unbind('click').click(fireclick);
    $('#clickme').unbind().click(fireclick);
    

提交回复
热议问题