鼠标:禁用右键和中键点击

mouse : disable right and middle button click

本文关键字:右键 鼠标      更新时间:2023-09-26

我用过这个,但它不起作用;firebug中没有错误:

$("div").live('mousedown', function(e) { 
   if( (e.which == 1) ) {
     return ;
   }if( (e.which == 3) ) {
     return false;
   }else if( (e.which == 2) ) {
      return false; 
   }
})

我可以禁用contextmenu右键,但我不知道如何处理中间键

您可以使用Javascript和/或HTML属性(实际上是Javascript事件处理程序)来完成此操作,如下所述

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

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

话虽如此,还是不要这样做。

为什么?因为它只会惹恼用户。此外,许多浏览器都有一个安全选项来禁止禁用右键(上下文)菜单。

不知道你为什么想要。如果您错误地认为可以通过这种方式保护源代码或图像,那么请再想一想:这是不可能的。

您的代码中缺少了一个else

$("div").live('mousedown', function(e) { 
   if( (e.which == 1) ) {
     return ;
   }else if( (e.which == 3) ) {
     return false;
   }else if( (e.which == 2) ) {
      return false; 
   }
})

使用下面的代码禁用鼠标右键和中键点击。

 //Disable the right mouse button click.
 document.oncontextmenu = function () {
    return false;
 };
 //Disable the middle mouse button click.
 document.onauxclick = function () {
    return false;
 };
<a href="https://stackoverflow.com">stackoverflow</a>