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

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

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



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


        
4条回答
  •  粉色の甜心
    2020-12-30 05:09

    Ok, solved. I just had to bind the handler to document:

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

    https://jsfiddle.net/6hm00xxv/1/

提交回复
热议问题