JavaScript:仅在特定元素内捕获右键单击并禁用菜单

发布于 2024-09-04 08:49:13 字数 285 浏览 2 评论 0原文

我编写了一个 jquery 脚本,其中屏幕上有一个小网格,并且使用拖放用户可以在网格上放置图块(捕捉到位)。目前,如果您将鼠标悬停在图块上,它会淡入旋转选项,但如果您可以右键单击进行旋转(使其更自然),我会更喜欢它。我知道完全阻止右键单击通常会让人皱眉,所以想知道是否可以仅在特定元素内,然后捕获该事件,在 JS 中执行某些操作并禁用上下文菜单? - 适用于所有浏览器。

顺便说一句,目前我正在使用 jQuery 实现效果,并使用自定义 javascript 进行拖放,是否值得考虑使用 jQuery 拖放插件?

非常感谢,

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,

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

妖妓 2024-09-11 08:49:14

“是否值得考虑用于拖放的 jQuery 插件?”

仅当您不希望您的应用程序在带有 safari 的 iPhone 操作系统(即包括 iPad)上使用时,请参阅 Safari 网页内容指南:处理事件

"is it worth looking at a jQuery plugin for drag and drop?"

Only if you don't intend your application to be used on the iPhone O.S with safari, i.e. including iPad, see Safari Web Content Guide: Handling Events

沧桑㈠ 2024-09-11 08:49:13

要捕获右键单击,您可以使用此 jquery:

$('#gridID').bind('contextmenu', function(e) {
   // do stuff here instead of normal context menu
   return false;
});

这适用于 chrome、firefox 和 safari。 尚未测试 IE。也适用于 IE。唯一需要注意的是它显然在 Opera 中不起作用。所以如果你能忍受这一点......

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...

云柯 2024-09-11 08:49:13

我不喜欢在网页上使用鼠标右键。但是,如果您确实想要这样做,您可以按照此处所述捕获鼠标右键。如果鼠标位于网格单元格上,您可以有条件地阻止鼠标右键(换句话说,在事件处理程序中返回 false)。

关于您的奖金问题: jquery ui 有拖放和拖拽功能。删除功能。使用它可能比自己动手更容易。

I'm not a fan of using the right mouse button on web pages. However, if you really want to do it, you could trap the right mouse button as described here. You could block the right mouse button (in other words return false in your event handler) conditionally if the mouse is over your grid cells.

Regarding your bonus question: jquery ui has drag & drop functionality. It's probably easier to use that than rolling your own.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文