jquery .off(): how to remove a certain click handler only?

前端 未结 4 1055
佛祖请我去吃肉
佛祖请我去吃肉 2020-12-30 04:04

I\'ve an element with two handler bound to it:



$(\'.pippo\').on(\'click\', function () {
             


        
4条回答
  •  隐瞒了意图╮
    2020-12-30 04:55

    Because you are calling .off for click event. It is removing all possible click events on that selected element. The trick is to define a handler and remove that particular handler only.

    function showPluto() {
      showMsg("pluto");
    };
    
    function showPippo() {
      showMsg("pippo");
    };
    
    function showMsg(text) {
      alert("showMsg called with text: " + text);
    };
    
    $('.pippo').on('click', showPippo);
    $('.pluto').on('click', showPluto);
    
    $('.dai').on('click', function() {
      $('.pippo').off('click', showPippo);
      alert("ok, removed");
    });
    

提交回复
热议问题