2012-08-11 82 views
0

所以标题说我怎么能做到这一点?如何防止右键点击弹出窗口

一些google搜索后,我得到这个代码放在一起:

$('.lines').mousedown(function(event) { 
    if(event.which == 1){ 
     console.log("left"); 
    }else{ 
     event.preventDefault(); 
     console.log("right"); 
    } 
}); 

首先,我需要确定哪些按钮被按下,因为我要使用不同的动作。但由于某种原因,event.preventDefault单击鼠标右键时不会执行此操作。

回答

1

请尝试:

$(document).ready(function() 
{ 
    $(document).bind("contextmenu",function(e){ 
     return false; 
    }); 
}); 

其实,这很烦人,当你不能在网站上使用鼠标右键。


您可以用同样的方法禁用它针对任何特定块:

$(document).ready(function() 
{ 
    $('#test').bind("contextmenu",function(e){ 
     return false; 
    }); 
});​ 

样本HTML:

<div id="test"> ​No right clicks allowed here! </div>​ 

而且样品CSS:

#test { width: 200px; height: 200px; background: red; } 

而且,最后,an example

+0

我不知道我可以绑定更多然后一个动作元素,他们可以一起工作,谢谢,现在我已经绑定了'mousedown'和'contextmenu',它完美的工作 – Linas 2012-08-11 19:05:17

+0

@Linas我很高兴帮帮我。 – 2012-08-11 19:06:30