Does anybody know how to unbind set of event handlers, but memorize them in order to bind them again later? Any suggestions?
有谁知道如何解绑定事件处理程序集,但是要记住它们,以便以后再绑定它们?有什么建议吗?
7 个解决方案
#1
23
There is a events element in the data of the item. This should get your started, you can read your elements and store the handlers in an array before unbinding. Comment if you need more help. I got this idea from reading the $.fn.clone method so take a look at that as well.
项目的数据中有一个events元素。这应该开始了,您可以在解绑定之前读取元素并将处理程序存储在一个数组中。如果你需要更多的帮助,请评论。我是通过读$.fn得到这个想法的。克隆方法,我们也来看看。
$(document).ready(function() {
$('#test').click(function(e) {
alert('test');
var events = $('#test').data("events");
$('#test').unbind('click', events.click[0]);
});
});
<a id="test">test</a>
#2
8
Here is how to achieve that, provides a storeEvents
and a restoreEvents
methods on a selection. storeEvents
takes a snapshot of the events on the moment it is called. restoreEvents
restores to the last previous snapshot. Might need to twist it a little for parameterizing the unbinding while restoring, maybe you'd like to keep the bound events after the last snapshot.
这里是如何实现这一点的方法,提供了一个存储事件和一个选择上的restoreEvents方法。storeEvents在事件被调用的那一刻对事件进行快照。恢复事件恢复到上一个快照。为了在恢复时参数化解绑定,可能需要稍微扭曲它,也许您希望在上次快照之后保留绑定事件。
(function($){
function obj_copy(obj){
var out = {};
for (i in obj) {
if (typeof obj[i] == 'object') {
out[i] = this.copy(obj[i]);
}
else
out[i] = obj[i];
}
return out;
}
$.fn.extend({
storeEvents:function(){
this.each(function(){
$.data(this,'storedEvents',obj_copy($(this).data('events')));
});
return this;
},
restoreEvents:function(){
this.each(function(){
var events = $.data(this,'storedEvents');
if (events){
$(this).unbind();
for (var type in events){
for (var handler in events[type]){
$.event.add(
this,
type,
events[type][handler],
events[type][handler].data);
}
}
}
});
return this;
}
});
})(jQuery);
#3
7
Since jQuery 1.4.2+ changes how event handlers are stored, this seems relevant:
由于jQuery 1.4.2+改变了事件处理程序的存储方式,这似乎是相关的:
The best way I found was to use event namespacing:
我发现最好的方法是使用事件命名空间:
var ary_handlers = [ fn_highlight, fn_onpress, fn_cleanup ];
for ( idx = 0; idx < ary_handlers.length; idx++ ){
$('#test').bind('click.foobar',ary_handlers[idx]);
}
// and then later:
$('#test').unbind('.foobar');
In the above example, all foobar events unbound. Notice that if you needed finer grain control, you could namespace each click handler and correlate to your array of handlers:
在上面的示例中,所有foobar事件都是未绑定的。注意,如果您需要更精细的谷物控制,您可以为每个单击处理程序命名空间,并与处理程序数组关联:
var ary_handlers = [ fn_highlight, fn_onpress, fn_cleanup ];
for ( idx = 0; idx < ary_handlers.length; idx++ ){
$('#test').bind('click.ns_' + String(idx), ary_handlers[idx]);
}
// and then later you could pick off a specific one to unbind
$('#test').unbind('.ns_2');
#4
2
There is now a good jQuery plugin called copyEvents, which copies events from one object to another. This could very easily be used to "save" events from one element and bring them back later. Just one more option :)
现在有一个很好的jQuery插件叫做copyEvents,它将事件从一个对象复制到另一个对象。这可以很容易地用于从一个元素“保存”事件,并在稍后将其返回。还有一个选择:)
#5
0
In order to unbind an event handler you need to pass the handler function into unbind(). So you already have the handler function, all you have to do is remember it.
为了解绑定事件处理程序,需要将处理程序函数传递给unbind()。你已经有了handler函数,你要做的就是记住它。
#6
0
You could use the event.handler
parameter:
你可以利用这个事件。处理程序参数:
$(document).ready(function() {
$('#test').click(function(e) {
e.preventDefault();
alert('test');
$('#test').unbind('click', e.handler);
//Do Something...
$('#test').click();
});
});
<a id="test">test</a>
event.handler
returns the current handler that has received the event, So by omitting it you could keep the other handlers.
事件。handler返回已接收事件的当前处理程序,因此通过省略它,可以保留其他处理程序。
#7
-2
Nick Craver appears to have the correct answer for jQuery 1.4.2+. He also includes a helpful fiddle. His solution allows you to retrieve all event handlers attached to a jQuery element and to figure out which handler they were attached to.
Nick Craver似乎对jQuery 1.4.2+有正确的答案。他还包括一把有用的小提琴。他的解决方案允许您检索附加到jQuery元素的所有事件处理程序,并确定它们附加到哪个处理程序。
#1
23
There is a events element in the data of the item. This should get your started, you can read your elements and store the handlers in an array before unbinding. Comment if you need more help. I got this idea from reading the $.fn.clone method so take a look at that as well.
项目的数据中有一个events元素。这应该开始了,您可以在解绑定之前读取元素并将处理程序存储在一个数组中。如果你需要更多的帮助,请评论。我是通过读$.fn得到这个想法的。克隆方法,我们也来看看。
$(document).ready(function() {
$('#test').click(function(e) {
alert('test');
var events = $('#test').data("events");
$('#test').unbind('click', events.click[0]);
});
});
<a id="test">test</a>
#2
8
Here is how to achieve that, provides a storeEvents
and a restoreEvents
methods on a selection. storeEvents
takes a snapshot of the events on the moment it is called. restoreEvents
restores to the last previous snapshot. Might need to twist it a little for parameterizing the unbinding while restoring, maybe you'd like to keep the bound events after the last snapshot.
这里是如何实现这一点的方法,提供了一个存储事件和一个选择上的restoreEvents方法。storeEvents在事件被调用的那一刻对事件进行快照。恢复事件恢复到上一个快照。为了在恢复时参数化解绑定,可能需要稍微扭曲它,也许您希望在上次快照之后保留绑定事件。
(function($){
function obj_copy(obj){
var out = {};
for (i in obj) {
if (typeof obj[i] == 'object') {
out[i] = this.copy(obj[i]);
}
else
out[i] = obj[i];
}
return out;
}
$.fn.extend({
storeEvents:function(){
this.each(function(){
$.data(this,'storedEvents',obj_copy($(this).data('events')));
});
return this;
},
restoreEvents:function(){
this.each(function(){
var events = $.data(this,'storedEvents');
if (events){
$(this).unbind();
for (var type in events){
for (var handler in events[type]){
$.event.add(
this,
type,
events[type][handler],
events[type][handler].data);
}
}
}
});
return this;
}
});
})(jQuery);
#3
7
Since jQuery 1.4.2+ changes how event handlers are stored, this seems relevant:
由于jQuery 1.4.2+改变了事件处理程序的存储方式,这似乎是相关的:
The best way I found was to use event namespacing:
我发现最好的方法是使用事件命名空间:
var ary_handlers = [ fn_highlight, fn_onpress, fn_cleanup ];
for ( idx = 0; idx < ary_handlers.length; idx++ ){
$('#test').bind('click.foobar',ary_handlers[idx]);
}
// and then later:
$('#test').unbind('.foobar');
In the above example, all foobar events unbound. Notice that if you needed finer grain control, you could namespace each click handler and correlate to your array of handlers:
在上面的示例中,所有foobar事件都是未绑定的。注意,如果您需要更精细的谷物控制,您可以为每个单击处理程序命名空间,并与处理程序数组关联:
var ary_handlers = [ fn_highlight, fn_onpress, fn_cleanup ];
for ( idx = 0; idx < ary_handlers.length; idx++ ){
$('#test').bind('click.ns_' + String(idx), ary_handlers[idx]);
}
// and then later you could pick off a specific one to unbind
$('#test').unbind('.ns_2');
#4
2
There is now a good jQuery plugin called copyEvents, which copies events from one object to another. This could very easily be used to "save" events from one element and bring them back later. Just one more option :)
现在有一个很好的jQuery插件叫做copyEvents,它将事件从一个对象复制到另一个对象。这可以很容易地用于从一个元素“保存”事件,并在稍后将其返回。还有一个选择:)
#5
0
In order to unbind an event handler you need to pass the handler function into unbind(). So you already have the handler function, all you have to do is remember it.
为了解绑定事件处理程序,需要将处理程序函数传递给unbind()。你已经有了handler函数,你要做的就是记住它。
#6
0
You could use the event.handler
parameter:
你可以利用这个事件。处理程序参数:
$(document).ready(function() {
$('#test').click(function(e) {
e.preventDefault();
alert('test');
$('#test').unbind('click', e.handler);
//Do Something...
$('#test').click();
});
});
<a id="test">test</a>
event.handler
returns the current handler that has received the event, So by omitting it you could keep the other handlers.
事件。handler返回已接收事件的当前处理程序,因此通过省略它,可以保留其他处理程序。
#7
-2
Nick Craver appears to have the correct answer for jQuery 1.4.2+. He also includes a helpful fiddle. His solution allows you to retrieve all event handlers attached to a jQuery element and to figure out which handler they were attached to.
Nick Craver似乎对jQuery 1.4.2+有正确的答案。他还包括一把有用的小提琴。他的解决方案允许您检索附加到jQuery元素的所有事件处理程序,并确定它们附加到哪个处理程序。