更新时间:2023-12-06 08:30:10
将处理程序缓存到变量。然后使用该引用绑定和解除绑定。
Cache the handler to a variable. Then bind and unbind using that reference.
而不是将您的click事件内联绑定:
Instead of binding your click event inline:
$('#test').bind('click', function(){
alert('hi!');
});
将函数声明为变量:
var clickHandle = function(){
alert('hi!');
};
然后使用变量名称绑定:
And then bind using the variable name:
$('#test').bind('click', clickHandle);
然后你可以取消绑定特定的点击处理程序:
Then you can unbind the specific click handler:
$('#test').unbind('click', clickHandle);
然后你仍然可以重新绑定相同的功能:
Then you can still re-bind the same function:
$('#test').bind('click', clickHandle);
快速查看来源。事件绑定到contextmenu,而不是单击。
Took a quick look at the source. The event is bound to contextmenu, not click.
您可以通过元素的data.events属性访问该函数(类似于j3frea所说的)。请查看此小提琴示例以获得完整分辨率。
You can access the function through the element's data.events property (similar to what j3frea was saying). Have a look at this fiddle example for a full resolution.
基本上你可以这样做:
var cachedHandler = null;
// disable
cachedHandler = $('#demo2').data('events').contextmenu[0].handler;
$('#demo2').unbind('contextmenu', cachedHandler);
// enable
$('#demo2').bind('contextmenu', cachedHandler);