如何禁用我的PHP网站上的右键单击?

时间:2017-08-14 06:38:07

标签: javascript php jquery

我正在使用php开发我的网站。现在,我要使用javascript禁用右键点击我的网页,但我不知道如何使用该脚本。

<script type="text/javascript">
$(document).ready(function () {
//Disable cut copy paste
$('body').bind('cut copy paste', function (e) {
    e.preventDefault();
});

//Disable mouse right click
$("body").on("context",function(e){
    return false;
});
});
</script>

3 个答案:

答案 0 :(得分:0)

尝试使用以下jquery

$(document).ready(function(){
    $(document).on("contextmenu",function(e){
        if(e.target.nodeName != "INPUT" && e.target.nodeName != "TEXTAREA")
             e.preventDefault();
     });
 });

答案 1 :(得分:0)

您可以查看以下事件:

$(body).click(function(e){
 if(e.which==1){
   //do the work here this checks for left mouse button.
  }
  });

答案 2 :(得分:0)

document.addEventListener("mousedown", function(e) {
    if (e.which === 3) { // if e is 1, it is left click, if it is 3 it is 
                         // right click
        alert("right click"); // this will pop up a message saying you 
                              // click the right button


    }
});

除了我的提醒,您只需使用您在上面的代码中使用的preventDefault。我刚刚添加了警报,因此您可以测试右键和左键单击以了解有关实际发生情况的更多信息。

但您也在询问如何使用该脚本。最好的方法是将您的javacript代码放在扩展名为.js的文件中,例如main.js,并将其包含在您的html文件中。您可以将它放在body标记的末尾,以确保在加载所有其他html元素后加载它,以便您的JavaScript可以在创建它们后找到它们。

您可以在</body>代码

结束前将其设置为正确
<script type="text/javascript" src="main.js"></script>

我假设您将javascript放在名为main.js的文件中

相关问题