我有一些对象创建附加到文档的一些事件。它们是常见的事件,如 mousemove、mousedown 等。
然后我想让 $(document).unbind('mousemove')
可以,但它可能会导致插件最终用户创建的一些事件崩溃或导致冲突 使用一些外部代码。
是否可以仅删除在某个范围内声明的所有事件?
喜欢:
$.fn.somePlugin = function() {
//here begins our scope
//declaring a lot of events
//some stuff
//remove events of this scope leaving other of same type untouched
}
或者还有其他方式来管理事件组吗?
最佳答案
您可以使用namespaced event处理程序
例如
$(el).on('click.myplugin', function(){...})//or any eventname.pluginname
然后
$(el).off('click.myplugin')
演示:Fiddle
关于javascript - jQuery 取消绑定(bind)范围内声明的事件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19098676/