HTML,JS禁止鼠标右键、禁止全选、复制、粘贴的方法
禁止鼠标右键、禁止全选、复制、粘贴;
oncontextmenu事件禁用右键菜单;
js代码:
document.oncontextmenu = function(){
event.returnValue = false;
}
// 或者直接返回整个事件
document.oncontextmenu = function(){
return false;
}
onselectstart事件禁用网页上选取的内容;
js代码:
document.onselectstart = function(){
event.returnValue = false;
}
// 或者直接返回整个事件
document.onselectstart = function(){
return false;
}
oncopy事件禁用复制;
js代码:
document.oncopy = function(){
event.returnValue = false;
}
// 或者直接返回整个事件
document.oncopy = function(){
return false;
}
以上三种事件,如果只想单纯的禁用鼠标右键,和复制粘贴,还可以将它们直接写到HTML中的body上面;
<body oncontextmenu = "return false" ></body>
<body onselectstart = "return false" ></body>
<body oncopy = "return false" ></body>
禁用鼠标事件
document.onmousedown = function(e){
if ( e.which == 2 ){// 鼠标滚轮的按下,滚动不触发
return false;
}
if( e.which==3 ){// 鼠标右键
return false;
}
}
禁用键盘中的ctrl、alt、shift
document.onkeydown = function(){
if( event.ctrlKey ){
return false;
}
if ( event.altKey ){
return false;
}
if ( event.shiftKey ){
return false;
}
}
关键就在
oncontextmenu='return false'
ondragstart='return false'
onselectstart ='return false'
onselect='document.selection.empty()'
oncopy='document.selection.empty()'
onbeforecopy='return false'
onmouseup='document.selection.empty()'
一个更简单的方法就是在<body>中加入如下的代码,这样鼠标的左右键都失效了.
topmargin="0"
oncontextmenu="return false" ondragstart="return false" onselectstart
="return false" onselect="document.selection.empty()"
oncopy="document.selection.empty()" onbeforecopy="return false"
onmouseup="document.selection.empty()"
1.禁止网页另存为:在<body>后面加入以下代码:
<noscript>
<iframe src="*.htm"></iframe>
</noscript>
2.禁止网页内容复制.粘贴:在<body>中加入以下代码:
<body
onmousemove=/HideMenu()/ oncontextmenu="return false"
ondragstart="return false" onselectstart ="return false"
onselect="document.selection.empty()"
oncopy="document.selection.empty()" onbeforecopy="return false"
onmouseup="document.selection.empty()">
参考页面:
https://www.9hospital.com.cn/djy_web/html/djy/jy_xxgk_fwgg/2014-11-06/Detail_1414.htm
分类:
前端
, 前端 / JavaScript
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 全网最简单!3分钟用满血DeepSeek R1开发一款AI智能客服,零代码轻松接入微信、公众号、小程
· .NET 10 首个预览版发布,跨平台开发与性能全面提升
· 《HelloGitHub》第 107 期
· 全程使用 AI 从 0 到 1 写了个小工具
· 从文本到图像:SSE 如何助力 AI 内容实时呈现?(Typescript篇)
2020-12-26 eclipse中如何找到jar包关联的源文件