屏蔽事件:
屏蔽选中、双击复制 该方法仅能屏蔽元素外的值:
document.onselectstart = function() {
return false;
}
屏蔽选中、双击复制、获取焦点:
document.onmousedown= function() {
return false;
}
屏蔽点击、选中(仅能屏蔽元素内的值)、获取焦点、悬浮事件(终极解决方案):
document.querySelectorAll('*').forEach(v => {
v.style.pointerEvents = 'none'
})
或使用CSS
* {
pointer-events: none;
}
屏蔽右键:
document.oncontextmenu = function() {
return false;
}
屏蔽复制:
document.oncopy = function(event) {
return false;
}
屏蔽粘贴:
document.onpaste= function(event) {
return false;
}
屏蔽剪切:
document.oncut= function(event) {
return false;
}
屏蔽F12:
document.onkeydown = function() {
if (window.event.keyCode == 123) {
alert("小样,你想干嘛");
return false;
}
}
所有屏蔽代码:
document.onselectstart = function() {
return false;
}document.οnmοusedοwn= function() {
return false;
}document.querySelectorAll('*').forEach(function(v) {
v.style.pointerEvents = 'none'
})document.oncontextmenu = function() {
return false;
}document.oncopy = function(event) {
return false;
}document.οnpaste= function(event) {
return false;
}document.oncut= function(event) {
return false;
}document.onkeydown = function() {
if (window.event.keyCode == 123) {
alert("小样,你想干嘛");
return false;
}
}
压缩后:
document.onselectstart=function(){return false};document.onmousedown=function(){return false};document.querySelectorAll("*").forEach(function(v){v.style.pointerEvents="none"});document.oncontextmenu=function(){return false};document.oncopy=function(event){return false};document.onpaste=function(event){return false};document.oncut=function(event){return false};document.onkeydown=function(){if(window.event.keyCode==123){alert("小样,你想干嘛");return false}};
解除限制:
(function() {
function remove(event) {
onevent = "on" + event;
if (window.addEventListener) {
window.addEventListener(event, function(e) {
for (var n = e.originalTarget; n; n = n.parentNode) {
n[onevent] = null;
}
}, true);
}
window[onevent] = null;
document[onevent] = null;
if (document.body) {
document.body[onevent] = null;
}
}
remove("selectstart")
remove("mousedown");
document.querySelectorAll("*").forEach(function (v) {
v.style.pointerEvents = "auto"
});
remove("contextmenu");
remove("copy");
remove("paste");
remove("cut");
remove("keydown");
remove("click");
remove("mouseup");
})();
压缩后:
(function(){function remove(event){onevent="on"+event;if(window.addEventListener){window.addEventListener(event,function(e){for(var n=e.originalTarget;n;n=n.parentNode){n[onevent]=null}},true)}window[onevent]=null;document[onevent]=null;if(document.body){document.body[onevent]=null}}remove("selectstart");remove("mousedown");document.querySelectorAll("*").forEach(function(v){v.style.pointerEvents="auto"});remove("contextmenu");remove("copy");remove("paste");remove("cut");remove("keydown");remove("click");remove("mouseup")})();