网页禁止右键与文本选择的JavaScript实现方案
作者:拾光时间:2026-10-27 20:38:03浏览:113
本文整理了多种使用JavaScript禁止网页右键菜单和禁止用户选择复制文本的实现方法。通过重写oncontextmenu事件可阻止右键菜单弹出,利用onselectstart或onmousedown事件组合可限制文本选择。代码兼容不同浏览器环境,并提供了排除特定表单元素的逻辑,适用于需要保护网页内容不被轻易复制的场景。
禁止网页右键与文本选择的代码实现
具体实现代码如下:
<!-- 禁止网页右键: -->
document.body.oncontextmenu=function rightClick(){ window.event.returnValue= false;}
<!-- 禁止网页另存为: -->
<noscript><iframe src=*.html></iframe></noscript>
<!-- 禁止选择文本: -->
<script type="text/javascript">
var omitformtags=["input", "textarea", "select"]
omitformtags=omitformtags.join("|")
function disableselect(e){
if (omitformtags.indexOf(e.target.tagName.toLowerCase())==-1)
return false
}
function reEnable(){
return true
}
if (typeof document.onselectstart!="undefined")
document.onselectstart=new Function ("return false")
else{
document.onmousedown=disableselect
document.onmouseup=reEnable
}
</script>
<!-- 禁用右键: -->
<script>
function stop(){
return false;
}
document.oncontextmenu=stop;
</script>作者最新文章
JS处理含Emoji或中文的字符串转数组乱码问题
2026-11-05 16:12:28
JS怎么使用展开运算符合并对象
2026-11-05 14:42:18
ES6+环境下判断JS对象为空的简洁写法
2026-11-05 12:12:23
怎么通过replacer函数自定义JS中JSON的格式化输出
2026-11-05 11:19:44
JS正则表达式基础:字面量与RegExp构造函数的区别及常用元字符
2026-11-05 11:16:58
