一、禁用瀏覽器退格鍵
摘抄自:https://www.cnblogs.com/wanggd/p/3164536.html
我們在真實的項目開發中經常會使用JS 對鍵盤上的一些按鍵進行禁用,常見的比如說退格鍵(backspace/ 后退鍵),我在一個項目中就遇到過在頁面編輯的時候禁用掉退格鍵,因為退格鍵會發生頁面后退,這樣編輯的內容都會失去了,非常的惡心人。ok ,廢話少說,直接上代碼。
<script type="text/javascript"> //處理鍵盤事件 禁止后退鍵(Backspace)密碼或單行、多行文本框除外 function forbidBackSpace(e) { var ev = e || window.event; //獲取event對象 var obj = ev.target || ev.srcElement; //獲取事件源 var t = obj.type || obj.getAttribute('type'); //獲取事件源類型 //獲取作為判斷條件的事件類型 var vReadOnly = obj.readOnly; var vDisabled = obj.disabled; //處理undefined值情況 vReadOnly = (vReadOnly == undefined) ? false : vReadOnly; vDisabled = (vDisabled == undefined) ? true : vDisabled; //當敲Backspace鍵時,事件源類型為密碼或單行、多行文本的, //並且readOnly屬性為true或disabled屬性為true的,則退格鍵失效 var flag1 = ev.keyCode == 8 && (t == "password" || t == "text" || t == "textarea") && (vReadOnly == true || vDisabled == true); //當敲Backspace鍵時,事件源類型非密碼或單行、多行文本的,則退格鍵失效 var flag2 = ev.keyCode == 8 && t != "password" && t != "text" && t != "textarea"; //判斷 if (flag2 || flag1) return false; } //禁止后退鍵 作用於Firefox、Opera document.onkeypress = forbidBackSpace; //禁止后退鍵 作用於IE、Chrome document.onkeydown = forbidBackSpace; </script>
使用方法:把上面的js代碼放到<head></head>之間就ok了
chorme支持
支持WinXP的最高版本是49.0.2623.112。下載:http://www.newasp.net/soft/71467.html,點下載壓縮包內的40.0.2214.91_chrome_installer.exe會自動下載安裝,安裝后的程序文件日期是2016-4-6。2016-04-14Google發布的Chrome 50不再支持XP。
二、禁止鼠標右鍵、全選、復制、粘貼
原文:https://www.cnblogs.com/happiness-mumu/p/6269465.html
禁用右鍵菜單
js代碼:
document.oncontextmenu = function(){ event.returnValue = false; } // 或者直接返回整個事件 document.oncontextmenu = function(){ return false; }
禁用網頁上選取的內容
js代碼:
document.onselectstart = function(){ event.returnValue = false; } // 或者直接返回整個事件 document.onselectstart = function(){ return false; }
oncopy事件禁用復制
js代碼:
document.oncopy = function(){ event.returnValue = false; } // 或者直接返回整個事件 document.oncopy = function(){ return false; }
以上三種事件,如果只想單純的禁用鼠標右鍵,和復制粘貼,還可以將它們直接寫到HTML中的body上面;
<body oncontextmenu = "return false" ></body> <body onselectstart = "return false" ></body> <body oncopy = "return false" ></body>
禁用鼠標事件
document.onmousedown = function(e){ if ( e.which == 2 ){// 鼠標滾輪的按下,滾動不觸發 return false; } if( e.which==3 ){// 鼠標右鍵 return false; } }
禁用鍵盤中的ctrl、alt、shift
document.onkeydown = function(){ if( event.ctrlKey ){ return false; } if ( event.altKey ){ return false; } if ( event.shiftKey ){ return false; } }
關鍵就在
oncontextmenu='return false' ondragstart='return false' onselectstart ='return false' onselect='document.selection.empty()' oncopy='document.selection.empty()' onbeforecopy='return false' onmouseup='document.selection.empty()'
一個更簡單的方法就是在<body>中加入如下的代碼,這樣鼠標的左右鍵都失效了.
禁止網頁另存為
在<body>后面加入以下代碼:
<noscript>
<iframe src="*.htm"></iframe>
</noscript>
禁止網頁內容復制.粘貼
在<body>中加入以下代碼:
<body onmousemove=/HideMenu()/ oncontextmenu="return false" ondragstart="return false" onselectstart ="return false" onselect="document.selection.empty()" oncopy="document.selection.empty()" onbeforecopy="return false" onmouseup="document.selection.empty()"></body>