首頁  >  文章  >  web前端  >  JavaScript程式碼實作禁止右鍵、禁選、禁貼、禁shift、禁ctrl、禁alt_javascript技巧

JavaScript程式碼實作禁止右鍵、禁選、禁貼、禁shift、禁ctrl、禁alt_javascript技巧

WBOY
WBOY原創
2016-05-16 15:32:061274瀏覽

廢話不多說了直接給大家貼程式碼了。

程式碼如下:

<script language="JavaScript">
<!--
//js禁用某些键的代码
//www.jb51.net
function key(){ 
if(event.shiftKey){
window.close();}
//禁止Shift
if(event.altKey){
window.close();}
//禁止Alt
if(event.ctrlKey){
window.close();}
//禁止Ctrl
return false;}
document.onkeydown=key;
if (window.Event)
document.captureEvents(Event.MOUSEUP);
function nocontextmenu(){
event.cancelBubble = true
event.returnValue = false;
return false;}
function norightclick(e){
if (window.Event){
if (e.which == 2 || e.which == 3)
return false;}
else
if (event.button == 2 || event.button == 3){
event.cancelBubble = true
event.returnValue = false;
return false;}
}
//禁右键
document.oncontextmenu = nocontextmenu; // for IE5+
document.onmou<a href="http://www.jb51.net/article/1141.html" target="_blank" class="infotextkey">sed</a>own = norightclick; // for all others
//-->
</script>


1. oncontextmenu="window.event.returnValue=false" 將徹底屏蔽滑鼠右鍵特效

no
可用於Table

2.

取消選取、防止複製

3. onpaste="return false" 不准貼上

4. oncopy="return false;" oncut="return false;" 防止複製

PS:JS防止後退,刷新,關閉的解決方案

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<html><head>
<title> New Document </title>
</head>
<script language="javascript">
function RunOnBeforeUnload() {window.onbeforeunload = function(){ return '将丢失未保存的数据!'; } }
</script>
<body onload="RunOnBeforeUnload()">
刷新,关闭,后退,F5 测试
</body>
</html>

雖然onbeforeunload這個事件已經Web標準被淘汰,但目前能實現這個效果的也就只有這個事件.還好瀏覽器都能很好的支援.

測試結果:

IE6.0,FireFox,Chrome透過

陳述:
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn