js 屏蔽禁止掉浏览器自带的选中、复制、剪切、粘贴功能

1. 屏蔽选中

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
<script>
document.onselectstart = function (event) {
if (window.event) {
event = window.event;
}
try {
var the = event.srcElement;
if (!((the.tagName == "INPUT" && the.type.toLowerCase() == "text") || the.tagName == "TEXTAREA")) {
return false;
}
return true;
} catch (e) {
return false;
}
}
</script>

2. 屏蔽复制

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
<script>
document.oncopy = function (event) {
if (window.event) {
event = window.event;
}
try {
var the = event.srcElement;
if (!((the.tagName == "INPUT" && the.type.toLowerCase() == "text") || the.tagName == "TEXTAREA")) {
return false;
}
return true;
} catch (e) {
return false;
}
}
</script>

3. 屏蔽剪切

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
<script>
document.oncut = function (event) {
if (window.event) {
event = window.event;
}
try {
var the = event.srcElement;
if (!((the.tagName == "INPUT" && the.type.toLowerCase() == "text") || the.tagName == "TEXTAREA")) {
return false;
}
return true;
} catch (e) {
return false;
}
}

4. 屏蔽粘贴

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
<script>
document.onpaste = function (event) {
if (window.event) {
event = window.event;
}
try {
var the = event.srcElement;
if (!((the.tagName == "INPUT" && the.type.toLowerCase() == "text") || the.tagName == "TEXTAREA")) {
return false;
}
return true;
} catch (e) {
return false;
}
}
</script>

js 屏蔽禁止掉浏览器自带的选中、复制、剪切、粘贴功能
https://github.com/chergn/chergn.github.io/f415136cb106/
作者
全易
发布于
2024年3月28日
许可协议