网页禁止右键与文本选择的JavaScript实现方案

作者:拾光

时间:2026-10-27

浏览:112

本文整理了多种使用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>

实用工具推荐

电子包浆

绿化、压缩、噪点和虚拟水印,自由调节做旧程度。

使用 ›
图片压缩

选择画质和输出格式,预览压缩前后的大小。

使用 ›
相关文章