How would you unbind all jQuery events from a particular namespace?

前端 未结 7 1275
半阙折子戏
半阙折子戏 2021-02-06 22:50

Is there a \"global\" unbind function in jQuery, such that I\'d be able to remove all bound events from a given namespace? eg:

// assume these are the events bou         


        
7条回答
  •  情书的邮戳
    2021-02-06 23:04

    You should add a class within the bind function

    $('#foo').bind('click.myNS', ...).addClass('removeLater');
    $('#bar').bind('keyup.myNS', ...).addClass('removeLater');
    $('#baz').bind('dblclick.myNS', ...).addClass('removeLater');  
    

    and afterwards

    $('.removeLater').unbind();
    

    If #foo #bar and #etc are all the same element or small set of them, you could also do:

    $('div, button').unbind();
    

    but if you have a bound event you don't want to remove that wouldn't be a good idea

提交回复
热议问题