如何在firefox中按下shift时禁用鼠标右键菜单?

时间:2014-03-31 11:42:13

标签: javascript jquery firefox

我试图禁用鼠标右键单击选项。所以我用contextmenu绑定函数来阻止它。这样可以正常工作但是当shiftmosue right click一起按下时,contextmenu绑定功能不会触发,但会显示contextmenu。意味着我没有得到警报,但它显示了菜单。

这是我试过的代码。

$(document).ready(function(){
    $(document).bind("contextmenu",function(e){
   alert('Context Menu event has fired!');
   return false;
}); 
});

为了捕捉换档按钮,按下鼠标并右键单击我正在执行下面的代码,但这并没有帮助。可能是我做错了。

$(document).ready(function(){

    $(document).bind("contextmenu",function(e){
   alert('Context Menu event has fired!');
   return false;
}); 

    var shift = false;
jQuery(document).on("keydown", function(event) {
            //check for shift key is pressed
        if (event.which === 16) { 
        shift = true;
                        }
});
jQuery(document).mousedown(function(e) {
     // e.which === 3 is for mouse right click
   if (e.which === 3 && shift === true) {
console.log("both action are triggered");
       return false; // how to stop the contextmenu action here
                           }
});
});

我尝试给e.preventDefault而不是返回false。我认为单击shift时,上下文菜单事件本身不会在firefox中触发。

如何在这种情况下为firefox禁用鼠标右击?任何帮助或线索都会有所帮助

JSFIDDLE 注意 这不是在chrome中发生的。这只发生在firefox中。这是一个错误吗?

2 个答案:

答案 0 :(得分:2)

这不是一个错误,它是一个功能!

http://www.whatwg.org/specs/web-apps/current-work/multipage/interactive-elements.html#context-menus

  

用户代理可以提供绕过上下文菜单的方法   处理模型,确保用户始终可以访问UA   默认上下文菜单。例如,用户代理可以处理   右键单击按住Shift键以使其按下的方式   不会触发contextmenu事件,而是始终显示   默认上下文菜单。

根据设计,您将无法在Firefox中执行此操作。它很烦人,特别是对于复杂的网络应用和游戏,但是它被硬编码到浏览器中,并且无法在javascript中禁用它(我知道)。

责怪标准,而不是Mozilla。

答案 1 :(得分:0)

Javascript code to disable mouse right click

 <script language="javascript">
    document.onmousedown=disableRightclick;
    status="Disabled";
    function disableRightclick(event)
    {
      if(event.button==2)
       {
         alert(status);
         return false;    
       }
    }
    </script>

在HTML Body标记上,将oncontextmenu属性设置为false。

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

免责声明:提供的链接是我写的博客。希望   这解决了这个问题。

相关问题