如何在JavaScript中禁用右键单击上下文菜单 [英] How to disable right-click context-menu in JavaScript

查看:126
本文介绍了如何在JavaScript中禁用右键单击上下文菜单的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

并不是说我试图阻止查看源代码或类似的任何蠢事,但我正在为某些元素制作一些自定义上下文菜单。

Not that I'm trying to prevent 'View Source' or anything silly like that, but I'm making some custom context menus for certain elements.

编辑:对答案的回答:我试过这个:

response to answers: I've tried this:

<a id="moo" href=''> </a>

<script type="text/javascript">
    var moo = document.getElementById('moo');

    function handler(event) {
        event = event || window.event;

        if (event.stopPropagation)
            event.stopPropagation();

        event.cancelBubble = true;
        return false;
    }

    moo.innerHTML = 'right-click here';

    moo.onclick = handler;
    moo.onmousedown = handler;
    moo.onmouseup = handler;
</script>


推荐答案

捕获 onContextMenu event,并在事件处理程序中返回false。

Capture the onContextMenu event, and return false in the event handler.

您还可以捕获click事件并使用<$ c $检查触发事件的鼠标按钮c> event.button ,无论如何在某些浏览器中。

You can also capture the click event and check which mouse button fired the event with event.button, in some browsers anyway.

这篇关于如何在JavaScript中禁用右键单击上下文菜单的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆