1. 程式人生 > >js禁用功能鍵

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();