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

前端 未结 7 1272
半阙折子戏
半阙折子戏 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:21

    You need to use jQuery's On and Off methods. Use document as the selector.

    $(document).off('.myNS');
    
    $(document).on('click.myNS','#foo', ...);
    $(document).on('keyup.myNS','#bar', ...);
    $(document).on('dblclick.myNS','#baz',''');
    

    then your methods could look like

    $(document).on('click.myNS','#foo', fooClicked);
    var fooClicked = function(e){
        var $this = $(e.target);
        // do stuff
    }
    

提交回复
热议问题