JavaScript:仅在特定元素内捕获右键单击和禁用菜单 [英] JavaScript: Capturing right click and disabling menu only within certain element

查看:112
本文介绍了JavaScript:仅在特定元素内捕获右键单击和禁用菜单的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经编写了一个jquery脚本,屏幕上有一个小网格,使用拖放功能可以将瓦片放在网格上(捕捉到位)。目前,如果您将鼠标悬停在瓦片上,则会在旋转的选项中消失,但是如果您可以右键单击旋转,我会更喜欢它(使其更自然)。我明白阻止右击完全是经常被皱眉的,所以想知道是否可能只是在一个特定的元素内,然后捕获该事件,在JS中做某事并禁用上下文菜单? - 在每个浏览器中都有效。



在旁注中,目前我正在使用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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆