Javascript 如何禁用鼠标右键单击网页?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3083798/
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
How to disable mouse right click on a web page?
提问by User 1034
I want to disable mouse right click on an HTML page. I have a page where user has to enter the details. I don't want the user to see the menu thats get displayed with the mouse right click. Rather I want to display a custom menu. I know there are some plugins available to do that. But my requirement does not need any plugins.
我想禁用鼠标右键单击 HTML 页面。我有一个页面,用户必须在其中输入详细信息。我不希望用户看到通过鼠标右键单击显示的菜单。相反,我想显示一个自定义菜单。我知道有一些插件可以做到这一点。但我的要求不需要任何插件。
回答by Andrea Zilio
It's unprofessional, anyway this will work with javascript enabled:
这是不专业的,无论如何这将在启用 javascript 的情况下工作:
document.oncontextmenu = document.body.oncontextmenu = function() {return false;}
You may also want to show a message to the user before returning false.
您可能还想在返回 false 之前向用户显示一条消息。
However I have to say that this should not be done generally because it limits users options without resolving the issue (in fact the context menu can be very easily enabled again.).
但是我不得不说,这通常不应该这样做,因为它限制了用户的选择而没有解决问题(事实上,上下文菜单可以很容易地再次启用。)。
The following article better explains whythis should not be done and what other actions can be taken to solve common related issues: http://articles.sitepoint.com/article/dont-disable-right-click
以下文章更好地解释了为什么不应该这样做以及可以采取哪些其他措施来解决常见的相关问题:http: //articles.sitepoint.com/article/dont-disable-right-click
回答by romiem
Firstly, if you are doing this just to prevent people viewing the source of your page - it won't work, because they can always use a keyboard shortcut to view it.
首先,如果您这样做只是为了防止人们查看您页面的源代码 - 它不会起作用,因为他们始终可以使用键盘快捷键来查看它。
Secondly, you will have to use JavaScript to accomplish this. If the user has JS disabled, you cannot prevent the right click.
其次,您必须使用 JavaScript 来完成此操作。如果用户禁用了 JS,则无法阻止右键单击。
That said, add this to your body tag to disable right clicks.
也就是说,将此添加到您的正文标签以禁用右键单击。
<body oncontextmenu="return false;">
回答by cHao
<body oncontextmenu="return false;">works for me in Google Chrome. Not sure about other browsers.
<body oncontextmenu="return false;">在谷歌浏览器中对我有用。不确定其他浏览器。
Note, all someone has to do is disable JavaScript in order to see the right-click menu anyway.
请注意,任何人所要做的就是禁用 JavaScript 以便无论如何都能看到右键单击菜单。
回答by rahul
You can use the oncontextmenuevent for doing this.
您可以使用oncontextmenu事件来执行此操作。
But if the user turns off javascript then you won't be able to handle this.
但是,如果用户关闭 javascript,那么您将无法处理此问题。
window.oncontextmenu = function () {
return false;
}
will disable right click menu.
将禁用右键菜单。
回答by codingbadger
There are plenty of examples of this which can be found via Google
有很多这样的例子可以通过谷歌找到
However, users can turn off Javascript to stop this highly annoying "feature". I think you should really think about this before implementing it. It isn't really going to protect your content (if that is what you are trying achieve).
但是,用户可以关闭 Javascript 来停止这个非常烦人的“功能”。我认为您应该在实施之前真正考虑这一点。它不会真正保护您的内容(如果这是您想要实现的目标)。
There is an article herethat illustrates just how annoying and pointless it is.
有文章在这里,说明它是多么讨厌和毫无意义的。
回答by nico
Please do not do that, it is very annoying.
请不要这样做,这很烦人。
The right menu is there for a reason, and it should be left there. Many browser extensions add entries to the right click menu and the user should be able to use it in any page he visits.
正确的菜单存在是有原因的,它应该留在那里。许多浏览器扩展向右键菜单添加条目,用户应该能够在他访问的任何页面中使用它。
Moreover you can use all of the functionality of the right click menu in other ways (keyboard shortcuts, browser menu etc etc etc) so blocking the right click menu has the only effect of annoying the user.
此外,您可以通过其他方式(键盘快捷键、浏览器菜单等)使用右键单击菜单的所有功能,因此阻止右键单击菜单只会使用户感到烦恼。
PS: If really you cannot resist the urge to block it at least do not put a popup saying "no right click allowed".
PS:如果您真的无法抗拒阻止它的冲动,至少不要弹出“不允许右键单击”的弹出窗口。
回答by John
Try this : write below code on body & feel the magic :)
试试这个:在身体上写下下面的代码并感受魔法:)
body oncontextmenu="return false"
回答by Simpal Kumar
window.oncontextmenu = function () {
return false;
}
might help you.
可能会帮助你。
回答by Nikunj Aggarwal
You can do so with Javascript and/or an HTML attribute (which is really a Javascript event handler anyway) as described here: http://www.codeproject.com/KB/aspnet/Disabling_the_right_click.aspx
您可以使用 Javascript 和/或 HTML 属性(无论如何这实际上是一个 Javascript 事件处理程序)来执行此操作,如下所述:http: //www.codeproject.com/KB/aspnet/Disabling_the_right_click.aspx
<script language="javascript">
document.onmousedown=disableclick;
status="Right Click Disabled";
Function disableclick(event)
{
if(event.button==2)
{
alert(status);
return false;
}
}
</script>
and
和
<body oncontextmenu="return false">
...
</body>
回答by thecodedeveloper.com
//Disable right click script via java script code
//通过java脚本代码禁用右键脚本
<script language=JavaScript>
//Disable right click script
var message = "";
///////////////////////////////////
function clickIE() {
if (document.all) {
(message);
return false;
}
}
function clickNS(e) {
if (document.layers || (document.getElementById && !document.all)) {
if (e.which == 2 || e.which == 3) {
(message);
return false;
}
}
}
if (document.layers) {
document.captureEvents(Event.MOUSEDOWN);
document.onmousedown = clickNS;
} else {
document.onmouseup = clickNS;
document.oncontextmenu = clickIE;
}
document.oncontextmenu = new Function("return false")
</script>

