javascript 禁用 Firefox 愚蠢的右键单击上下文菜单
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/16377381/
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 Firefox's silly right click context menu
提问by CHRIS
I am making an HTML 5 game which requires the use of right click to control the player.
我正在制作一个 HTML 5 游戏,它需要使用右键单击来控制玩家。
I have been able to disable the right click context menu by doing:
我已经能够通过执行以下操作禁用右键单击上下文菜单:
<body oncontextmenu="return(false);">
Then it came to my attention that if you hold shift and right click, a context menu still opens in Firefox!
然后我注意到如果你按住 shift 并右键单击,一个上下文菜单仍然会在 Firefox 中打开!
So I disabled that by adding this JS as well:
所以我也通过添加这个 JS 来禁用它:
document.onclick = function(e) { if(e.button == 2 || e.button == 3) { e.preventDefault(); e.stopPropagation(); return(false); } };
However, if you hold shift, and then double right click in Firefox it still opens!
但是,如果您按住 shift,然后在 Firefox 中双击它仍然会打开!
Please tell me how to disable this bloody thing once and for all (I'm even willing to revert to some obscure, hacky, and unpractical solution, as long as it works).
请告诉我如何一劳永逸地禁用这个该死的东西(我什至愿意恢复到一些晦涩、笨拙和不切实际的解决方案,只要它有效)。
采纳答案by dainichi
You will never be able to entirely disable the context menu in all cases, as firefox has a setting that allows the user to tell the browser to ignore such hijinx as you are trying to pull.
Note: I'm on a mac, but this setting is in pretty uch the same place over all platforms.
您永远无法在所有情况下完全禁用上下文菜单,因为 Firefox 有一个设置,允许用户告诉浏览器忽略您尝试拉取的此类 hijinx。
注意:我使用的是 mac,但此设置在所有平台上都处于相同的位置。
That being said, try event.preventDefault() (see Vikash Madhow's comment on this other SO question: How to disable right-click context-menu in javascript)
话虽如此,请尝试 event.preventDefault() (请参阅 Vikash Madhow 对另一个 SO 问题的评论: How to disable right-click context-menu in javascript)
回答by icl7126
There is actually example in official documentationthat blocks directly context menu event:
官方文档中实际上有示例直接阻止上下文菜单事件:
document.oncontextmenu = function () { // Use document as opposed to window for IE8 compatibility
return false;
};
window.addEventListener('contextmenu', function (e) { // Not compatible with IE < 9
e.preventDefault();
}, false);
回答by spksa
document.ondblclick = function(e) {
if(e.button == 2 || e.button == 3) {
e.preventDefault();
e.stopPropagation();
return(false);
}
};