js禁用功能键

 1 方法一:
 2 // 禁用右键菜单、复制、选择
 3 $(document).bind("contextmenu copy selectstart", function() {
 4   return false;
 5 });
 6 
 7 方法二:
 8 // 禁用Ctrl+C和Ctrl+V(所有浏览器均支持)
 9 $(document).keydown(function(e) {
10   if(e.ctrlKey && (e.keyCode == 65 || e.keyCode == 67)) {
11     return false;
12   }
13 });
14 
15 方法三:
16 // 设置CSS禁止选择(如果写了下面的CSS则不需要这一段代码,新版浏览器支持)
17 $(function() {
18   $("body").css({
19     "-moz-user-select":"none",
20     "-webkit-user-select":"none",
21     "-ms-user-select":"none",
22     "-khtml-user-select":"none",
23     "-o-user-select":"none",
24     "user-select":"none"
25   });
26 });
27 
28 方法四:防止禁用JavaScript后失效,可以写在CSS中(新版浏览器支持,并逐渐成为标准):
29 
30 body {
31   -moz-user-select:none; /* Firefox私有属性 */
32   -webkit-user-select:none; /* WebKit内核私有属性 */
33   -ms-user-select:none; /* IE私有属性(IE10及以后) */
34   -khtml-user-select:none; /* KHTML内核私有属性 */
35   -o-user-select:none; /* Opera私有属性 */
36   user-select:none; /* CSS3属性 */
37 }
38 
39 方法五:
40 function donotCopy() {
41     $('html').bind('contextmenu', function (event) {
42         event.returnValue = false;
43         event.preventDefault();
44         return false;
45     });
46 
47     $('html').keydown(function (e) {
48         if (e.ctrlKey && (e.keyCode == 65 || e.keyCode == 67)) {
49             return false;
50         }
51     });
52 
53 }
54 donotCopy();

猜你喜欢

转载自www.cnblogs.com/slmdr9/p/9881703.html