2017-08-14 106 views
-2

我正在使用php开发我的网站。现在我打算禁用右键点击我的网页使用JavaScript,但我不知道如何使用脚本。如何禁用右键点击我的PHP网站?

<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> 
+5

**话虽这么说:DON” T DO IT。** – guradio

回答

0

尝试使用下面的jQuery

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

您可以使用事件查看类似如下:

$(body).click(function(e){ 
if(e.which==1){ 
    //do the work here this checks for left mouse button. 
    } 
    }); 
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