Javascript 在 chrome 中禁用右键单击菜单
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/6789843/
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 right-click menu in chrome
提问by Chris
I'm writing aWebGL game and want to use right-click as a control. However, it throws up a menu. Is it possible to disable that? I've tried
我正在编写一个WebGL 游戏并希望使用右键单击作为控件。但是,它会弹出一个菜单。是否可以禁用它?我试过了
}
else if (event.which == 2 || event.which == 3)
{
doRightClickControl();
event.preventDefault();
event.stopPropagation();
return false;
}
Thanks dknaack for the hint. I got it to work like this:
感谢 dknaack 的提示。我让它像这样工作:
window.oncontextmenu = function(event) {
event.preventDefault();
event.stopPropagation();
return false;
};
采纳答案by bennedich
Using jQuery for this purpose only is overkill. This will do the trick:
仅为此目的使用 jQuery 是多余的。这将解决问题:
(function () {
var blockContextMenu, myElement;
blockContextMenu = function (evt) {
evt.preventDefault();
};
myElement = document.querySelector('#myElement');
myElement.addEventListener('contextmenu', blockContextMenu);
})();
myElement can be replaced with window.
myElement 可以替换为 window。
回答by LabLogic
Use this:
用这个:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<head>
<title>disable rightclick menu - LabLogic</title>
</head>
<body>
<script language="javascript" type="text/javascript">
document.oncontextmenu=RightMouseDown;
document.onmousedown = mouseDown;
function mouseDown(e) {
if (e.which==3) {//righClick
alert("Disabled - do whatever you like here..");
}
}
function RightMouseDown() { return false;}
</script>
</body>
</html>
Tested in chrome 17, works with other new browsers as well
在 chrome 17 中测试,也适用于其他新浏览器