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

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

    Truly you are looking for a magical global function but as always with magic you can only get illusion, in this case, of simplicity.

    There is also jQuery solution using .live() or .delegate() (which is only variant of live()).

    Using $("body").delegate("#foo", "click.myNs", func) you also bind event and using $("#body").undelegate(".myNs") you unbind it. I use here "body" element but ofcourse you can use any element that is ancestor of all elements you need binding on.

    It will make your "unbinding" work faster but again it will make your events respond slower.

提交回复
热议问题