Jquery/JS 阻止浏览器中的右键菜单
我的 div 带有右键单击弹出菜单:
// Attatch right click event to folder for extra options
$('#fBox' + folderID).mousedown(function(event) {
if (event.which == 3) {
// Set ID
currRClickFolder = folderID;
// Calculate position to show popup menu
var height = $('#folderRClickMenu').height();
var width = $('#folderRClickMenu').width();
leftVal = event.pageX - (width / 2) + "px";
topVal = event.pageY - (height) + "px";
$('#folderRClickMenu').css({ left: leftVal, top: topVal }).show();
}
});
但是此元素的浏览器仍然会弹出默认菜单(复制/粘贴/属性等)。有办法禁用这个吗?我尝试过 return false 但运气不佳。
I have my div with a right click popup menu:
// Attatch right click event to folder for extra options
$('#fBox' + folderID).mousedown(function(event) {
if (event.which == 3) {
// Set ID
currRClickFolder = folderID;
// Calculate position to show popup menu
var height = $('#folderRClickMenu').height();
var width = $('#folderRClickMenu').width();
leftVal = event.pageX - (width / 2) + "px";
topVal = event.pageY - (height) + "px";
$('#folderRClickMenu').css({ left: leftVal, top: topVal }).show();
}
});
But the browser for this element still pops up the default menu (copy/paste/properties etc). Any way to disable this? I've tried return false but not luck.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(12)
这是我最近遇到问题时使用的一种方法(也使用了一点 jQuery)。由于 mousedown 事件发生在 contextmenu 之前,因此这个技巧似乎可以捕获它,即附加一个主体级别的 oncontextmenu 处理程序,以在 mousedown 事件中暂时返回 false,执行您想要的操作,然后作为重要部分,记住之后删除处理程序。
这只是我提取的代码的一部分,作为示例...
当我的 showoptions() rtn 完成时,会运行一个回调函数,并再次调用disable-rtn,但使用“false”:
这是我的disableContextMenu() rtn :
Here's a way I used recently (using a little jQuery too,) when I was running into a problem with it. Since the mousedown event occurs before the contextmenu, this trick seems to catch it, which is attaching a body level oncontextmenu handler to return false temporarily in the mousedown event, perform your desired action, then as an important part, remember to remove the handler afterward.
This is just part of my code extracted out, as an example...
When my showoptions() rtn finishes, a callback function is run and it calls the disable-rtn again, but with 'false':
Here's my disableContextMenu() rtn:
有许多 Javascript 代码片段可用于禁用右键单击上下文菜单,但 JQuery 使事情变得更加容易:
There’s many Javascript snippets available to disable right-click contextual menu, but JQuery makes things a lot easier:
您可以通过附加 oncontextmenu="return false;" 来禁用右键单击。到你的身体标签。
You can disable the right click by appending oncontextmenu="return false;" to your body tag.
您可以禁用任何您想要的元素上的上下文菜单:
要完全禁用页面上的上下文菜单(感谢 Ismail),请使用以下命令:
You can disable context menu on any element you want:
To disable context menu on the page completely (thanks to Ismail), use the following:
一行 jQuery:
One jQuery line:
试试这个:
Try this:
尝试...
如果您有这些框的动态创建,那么...
Try...
if you have any dynamic creation of these boxes then...
我同意@aruseni,在主体级别阻止 oncontextmenu 您将避免在页面中每个元素的右键单击上使用标准上下文菜单。
但如果您想进行更精细的控制怎么办?
我遇到了类似的问题,我想我已经找到了一个很好的解决方案:为什么不将上下文菜单代码直接附加到您想要处理的特定元素的
contextmenu
事件中?像这样的事情:因此,您可以避免处理两个不同的事件,只是为了捕获上下文菜单并对其进行自定义:)
当然,这假设您不介意在有人单击您未选择的元素时显示标准上下文菜单。您还可以根据用户右键单击的位置显示不同的上下文菜单
..HTH
I agree with @aruseni, blocking oncontextmenu at the body level you'll avoid the standard context menu on the right click for every element in the page.
But what if you want to have a finer control?
I had a similar issue and I thought I've found a good solution: why not attaching directly your context menu code to the
contextmenu
event of the specific element(s) you want to deal with? Something like this:Thus you avoid handling two different events just to capture the context menu and customize it :)
Of course this assumes you don't mind having the standard context menu displayed when someone clicks the elements you didn't select. You might as well show different context menus depending on where users right-click..
HTH
现在,这是浏览器的默认行为,用于禁用交替单击覆盖。每个用户都必须在最新的浏览器中允许这种行为。例如,我不允许这种行为,因为我总是想要默认的弹出菜单。
This is a default behavior of browsers now to disable the alternate-click override. Each user has to allow this behavior in recent browsers. For instance, I don't allow this behavior as I always want my default pop-up menu.
使用 jQuery:
或禁用整个页面上的上下文菜单:
Using jQuery:
Or disable context menu on the whole page:
为我
jQuery 完成了这项工作:)
For me
jQuery does the job :)