<!DOCTYPE>
<HTML>
<HEAD>
<SCRIPT>
/**
*兼容的事件处理
*
*target:目标元素
*type:操作类型
*event:操作触发的事件
**/
function addEventListener(target, type, event) {
if(target.addEventListener) {
target.addEventListener(type, event);
} else if(target.attachEvent) {
target.attachEvent("on" + type, event);
} else {
target["on" + type] = event;
}
}
/**
*禁止鼠标右键
*
*event:事件对象
**/
function rclick(event) {
//鼠标按键确认
var button = event.button;//鼠标点击;0右键,1中键,2右键;IE:1右,4中,2右
//alert(button);
if(button == 2) {
alert("右键禁止");
event.preventDefault();
}
}
/**
*简单的鼠标左键测试,禁止右键不对左键产生作用
**/
function btnClick() {
alert("button");
}
/**
*禁止ctrl+v
*
*event:事件对象
**/
function key_down(event) {
//键盘按键确认
var code = event.keyCode || event.which;//keyCode的C是大写
//event.ctrlKey, event.shiftKey, event.altKey,大写的K
if(event.ctrlKey && code == 86) {//ctrl+v
//按下ctrl时code是17,ctrlKey是true;再按下V时code是86,由于ctrl没有释放所以ctrlKey还是true.
//如果只用code来判断的话,将在ctrl按下时触发,不符合ctrl+v
alert("禁止黏贴");
event.preventDefault();
}
}
window.onload = function() {
addEventListener(document.body, "contextmenu", rclick);//contextmenu会覆盖鼠标右键
addEventListener(document.body, "mousedown", rclick);//鼠标右键事件
addEventListener(document.getElementById("btn"), "click", btnClick);
addEventListener(document.getElementById("inputText"), "keydown", key_down);
};
</SCRIPT>
</HEAD>
<BODY>
<p>abcdefg</p>
<input type="button" value="点击" id="btn">
<input type="text" id="inputText" size=20>
</BODY>
</HTML>
JS鼠标右键,ctrl+v
最新推荐文章于 2023-09-12 09:58:53 发布