如何禁用网页上的鼠标右键单击?

发布于 2024-09-06 06:19:09 字数 115 浏览 2 评论 0原文

我想禁用鼠标右键单击 HTML 页面。 我有一个页面,用户必须在其中输入详细信息。 我不希望用户看到通过鼠标右键单击显示的菜单。相反,我想显示一个自定义菜单。我知道有一些插件可以做到这一点。但我的要求不需要任何插件。

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.

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

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

发布评论

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

评论(10

⒈起吃苦の倖褔 2024-09-13 06:19:09

这是不专业的,无论如何这将在启用 javascript 的情况下工作:

document.oncontextmenu = document.body.oncontextmenu = function() {return false;}

您可能还想在返回 false 之前向用户显示一条消息。

然而我不得不说,一般不应该这样做,因为它限制了用户的选项而不解决问题(事实上,可以很容易地再次启用上下文菜单。)。

以下文章更好地解释了为什么不应该这样做,以及可以采取哪些其他措施来解决常见的相关问题:
https://www.sitepoint.com/dont-disable-right-click/< /a>

It's unprofessional, anyway this will work with javascript enabled:

document.oncontextmenu = document.body.oncontextmenu = function() {return false;}

You may also want to show a message to the user before returning 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 why this should not be done and what other actions can be taken to solve common related issues:
https://www.sitepoint.com/dont-disable-right-click/

神也荒唐 2024-09-13 06:19:09

首先,如果您这样做只是为了防止人们查看您页面的源代码,那么这是行不通的,因为他们始终可以使用键盘快捷键来查看它。

其次,您必须使用 JavaScript 来完成此任务。如果用户禁用了JS,则无法阻止右键。

也就是说,将其添加到您的正文标记中以禁用右键单击。

<body oncontextmenu="return false;">

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.

That said, add this to your body tag to disable right clicks.

<body oncontextmenu="return false;">
浪荡不羁 2024-09-13 06:19:09

在 Google Chrome 中适用于我。不确定其他浏览器是否如此。

请注意,人们所要做的就是禁用 JavaScript 才能看到右键菜单。

<body oncontextmenu="return false;"> works for me in Google Chrome. Not sure about other browsers.

Note, all someone has to do is disable JavaScript in order to see the right-click menu anyway.

顾北清歌寒 2024-09-13 06:19:09

您可以使用 oncontextmenu 事件来执行此操作。

但是如果用户关闭了 javascript 那么你将无法处理这个问题。

window.oncontextmenu = function () {
   return false;
}

将禁用右键菜单。

You can use the oncontextmenu event for doing this.

But if the user turns off javascript then you won't be able to handle this.

window.oncontextmenu = function () {
   return false;
}

will disable right click menu.

沉默的熊 2024-09-13 06:19:09

请不要这样做,这非常烦人。

右侧菜单的存在是有原因的,它应该留在那里。
许多浏览器扩展将条目添加到右键菜单中,用户应该能够在他访问的任何页面中使用它。

此外,您可以通过其他方式使用右键单击菜单的所有功能(键盘快捷键、浏览器菜单等),因此阻止右键单击菜单的唯一效果是烦扰用户。

PS:如果你真的无法抗拒阻止它的冲动,至少不要弹出一个“不允许右键单击”的弹出窗口。

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

皓月长歌 2024-09-13 06:19:09

有很多这样的例子,可以通过 Google< 找到/a>

但是,用户可以关闭 Javascript 来停止这个非常烦人的“功能”。我认为在实施之前你应该认真考虑一下这一点。它并不会真正保护您的内容(如果这是您想要实现的目标)。

这里有一篇文章说明了它是多么烦人和毫无意义。

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

There is an article here that illustrates just how annoying and pointless it is.

╰ゝ天使的微笑 2024-09-13 06:19:09

您可以使用 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>

<body oncontextmenu="return false">
...
</body>

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

<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>
苏大泽ㄣ 2024-09-13 06:19:09
window.oncontextmenu = function () {
return false;
}

可能对你有帮助。

window.oncontextmenu = function () {
return false;
}

might help you.

携君以终年 2024-09-13 06:19:09

试试这个:在 body & 上写下下面的代码感受魔法:)

body oncontextmenu="return false"

Try this : write below code on body & feel the magic :)

body oncontextmenu="return false"
箜明 2024-09-13 06:19:09

//通过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>

点击此处查看semo

//Disable right click script via java script code

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

Click here to see semo

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