{
    分享网正式开通,我们为大家提供免费资源,欢迎大家踊跃投稿!

禁止复制网页内容的方法(含PC端和移动端)

在网页的Html中,如果想实现禁止鼠标右键、禁止全选、复制、粘贴,可以考虑JS来实现,方式有很多种。

PC端禁止复制网页内容的方法

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>
 

禁用鼠标事件,js代码:


document.onmousedown = function(e){
        if ( e.which == 2 ){// 鼠标滚轮的按下,滚动不触发
                return false;
        }
        if( e.which==3 ){// 鼠标右键
                return false;
        }
}
 

禁用键盘中的ctrl、alt、shift,js代码:


document.onkeydown = function(){
        if( event.ctrlKey ){
                return false;
        }
        if ( event.altKey ){
                return false;
        }
        if ( event.shiftKey ){
                return false;
        }
}
 

有一个更简单的方法就是在<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()"
 

想要网页另存为:在<body>后面加入以下代码:


<noscript>
<iframe src="*.htm"></iframe>
</noscript>
 

禁止网页内容复制.粘贴:在<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()">
 

移动端禁止复制网页内容的方法

web端,只需在css中加入代码就可以了:


*{
      -webkit-touch-callout:none;
      -webkit-user-select:none;
      -khtml-user-select:none;
      -moz-user-select:none;
      -ms-user-select:none;
      user-select:none;
      }
 

以上代码也包括了禁止了input的输入,所以要单加一段代码开放input的权限:


input {
          -webkit-user-select:auto;
}
 

另附上一个app h5长安拷贝的解决方法(没有验证过,仅供参考),屏蔽掉长按事件 因为webview长按时将会调用系统的复制控件:


mWebView.setOnLongClickListener(new OnLongClickListener() {
    @Override
    public boolean onLongClick(View v) {
    return true;
      }
          });
 
资源均来自第三方,谨慎下载,前往第三方网站下载


爱资源分享网 , 版权所有丨本站资源仅限于学习研究,严禁从事商业或者非法活动!丨本网站采用BY-NC-SA协议进行授权
转载请注明原文链接:禁止复制网页内容的方法(含PC端和移动端)
喜欢 ()分享 (0)