JavaScript:仅在特定元素内捕获右键单击和禁用菜单 [英] JavaScript: Capturing right click and disabling menu only within certain element
问题描述
在旁注中,目前我正在使用jQuery的效果和自定义javascript进行拖放,是否值得一看jQuery插件非常感谢,
为了捕获右键单击,您可以使用此jquery:
$('#gridID')bind('contextmenu',function e){
//在这里做东西而不是正常的上下文菜单
return false;
});
这适用于chrome,firefox和safari。 没有测试IE 。在IE中也可以。只有注意到它在Opera中不起作用。所以,如果你可以生活在... ...
I have coded a jquery script where there is a small grid on screen and using drag and drop users can place tiles on the grid (snaps in place). Currently if you hover over a tile it fades in the option to rotate, but I would much prefer it if you could right click to rotate (making it more natural). I understand blocking right click completely is often frowned upon so was wondering if it was possible just within a particular element, then capturing that event, doing something in JS and disabling the context menu? - that works in every browser.
On a side note, currently I am using jQuery for effects and custom javascript for drag and drop, is it worth looking at a jQuery plugin for drag and drop?
Many thanks,
For capturing the right click, you can use this jquery:
$('#gridID').bind('contextmenu', function(e) {
// do stuff here instead of normal context menu
return false;
});
This works in chrome, firefox, and safari. Haven't tested IE. Works in IE too. Only caveat is it doesn't work in Opera apparently. So if you can live with that...
这篇关于JavaScript:仅在特定元素内捕获右键单击和禁用菜单的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!