你如何解除特定命名空间中的所有jQuery事件的绑定?
问题描述:
在jQuery中是否有全局解除绑定功能,以便我能够从给定的命名空间中删除所有绑定的事件?例如:
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 bound to different elements
$('#foo').bind('click.myNS', ...);
$('#bar').bind('keyup.myNS', ...);
$('#baz').bind('dblclick.myNS', ...);
// magic occurs here...
$.magicalGlobalUnbindFunction('.myNS');
// ...and afterwards, the three binds from above are gone
我所看到的所有示例都取消绑定,需要先选择一些元素。我猜,在技术上,你可以做 $('*')。unbind('。myNS')
,但这似乎非常效率低下。 / p>
All the examples I've seen for unbind require some elements to be selected first. I guess that technically, you could do $('*').unbind('.myNS')
, but that seems very inefficient.
答
您可以将 myNS
添加为每个元素的类,你想取消绑定事件。
You could add myNS
as a class to each of the elements where you want to unbind the events.