javascript 禁用和启用 jQuery 上下文菜单
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4728957/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Disable and enable jQuery context menu
提问by Upvote
I use
我用
$('#test').unbind('click');
to remove the click event on the #test item. How do I make the item clickable again?
删除#test 项目上的点击事件。如何使项目再次可点击?
Actually I have a table. On click event a context menu appears. But if there are no entries the menu has to be disabled. So I use unbind above. Since the context menu is generated by a plugin I do not know how to make it clickable again.
其实我有一张桌子。单击事件时会出现上下文菜单。但如果没有条目,则必须禁用菜单。所以我在上面使用unbind。由于上下文菜单是由插件生成的,我不知道如何使其再次可点击。
Any ideas?
有任何想法吗?
Update:this is how the context menu is set up
更新:这是上下文菜单的设置方式
$('#contacts tbody tr').contextMenu('myMenu1', {
bindings: {
'sms': function(t) {},
'delete': function(t) {}
}
});
Since I am still not sure how to solve my problem I will describe it a little more. I use the lightweight context-menu pluginin jQuery to display context menus.
由于我仍然不确定如何解决我的问题,因此我将对其进行更多描述。我使用jQuery 中的轻量级上下文菜单插件来显示上下文菜单。
#contacts tbody tr
are the table rows and myMenu1is the context menu that appears on tr click.
是表格行,myMenu1是点击 tr 时出现的上下文菜单。
On my page I have a table. Each row has its own context menu, well always the same but function(t)referes always to the clicked row.
在我的页面上,我有一张桌子。每一行都有自己的上下文菜单,总是一样的,但function(t)总是指向点击的行。
Well, the table may be empty so I want to disable the context menu. I believe there are may ways to do that. One is to unbind the click event, this does not work for me.
好吧,表格可能是空的,所以我想禁用上下文菜单。我相信可能有办法做到这一点。一种是解除点击事件的绑定,这对我不起作用。
I hope anyone has an idea.
我希望有人有想法。
回答by Josiah Ruddell
Cache the handler to a variable. Then bind and unbind using that reference.
将处理程序缓存到变量。然后使用该引用绑定和解除绑定。
Instead of binding your click event inline:
而不是内联绑定您的点击事件:
$('#test').bind('click', function(){
alert('hi!');
});
Declare the function to a variable:
将函数声明为变量:
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-bindthe same function:
然后你仍然可以重新绑定相同的功能:
$('#test').bind('click', clickHandle);
Took a quick look at the source. The event is bound to contextmenu, not click.
快速浏览了一下源码。该事件绑定到上下文菜单,而不是单击。
You can access the function through the element's data.events property (similar to what j3frea was saying). Have a look at this fiddle examplefor a full resolution.
您可以通过元素的 data.events 属性访问该函数(类似于 j3frea 所说的)。看看这个小提琴示例以获得完整的分辨率。
Essentially you can do this:
基本上你可以这样做:
var cachedHandler = null;
// disable
cachedHandler = $('#demo2').data('events').contextmenu[0].handler;
$('#demo2').unbind('contextmenu', cachedHandler);
// enable
$('#demo2').bind('contextmenu', cachedHandler);
回答by jcuenod
Take a look at this question Rebind DOM Event with jQuery
看看这个问题Rebind DOM Event with jQuery
Josiah's solution is preferable but if you really wanted to unbind the click event entirely I believe you could do this:
Josiah 的解决方案更可取,但如果您真的想完全解除单击事件的绑定,我相信您可以这样做:
var storedClick = $('#test').data('events').click[0].handler;
$('#test').unbind('click');
$('#test').bind('click', storedClick);
Remember that data('events').click is an array so you would need to store the handler for every member of the array.
请记住 data('events').click 是一个数组,因此您需要为该数组的每个成员存储处理程序。

