Home > Web Front-end > JS Tutorial > body text

js related ban

高洛峰
Release: 2016-10-15 17:47:10
Original
1156 people have browsed it

遇到网页上有精美图片或者精彩文字想保存时,通常大家都是选中目标后按鼠标右键,在弹出菜单中选择“图片另存为”或“复制”来达到我们的目的。但是,目前有许多网页都屏蔽了鼠标右键,那么用js如何实现禁止鼠标右键的功能呢?

1.与禁止鼠标右键相关的JS说明

 <script type="text/javascript">
     document.oncontextmenu=new Function("event.returnValue=false;");
     document.onselectstart=new Function("event.returnValue=false;");
 </script>
Copy after login

2.禁止鼠标右键火狐失灵

<!DOCTYPE html>
<html>
<head>
    <title>禁止鼠标右键</title>
    <meta charset="utf-8">
</head>
<body>
    <div class="poo">这个页面不能使用鼠标右键</div>
    <!-- 禁止鼠标右键 -->
    <script type="text/javascript">
        if (window.Event){  
            document.captureEvents(Event.MOUSEUP);  
         }

        function nocontextmenu(){  
            event.cancelBubble = true  
            event.returnValue = false;  
            return false;  
        }  
          
        function norightclick(e) { 
        
            if (window.Event) {
                if (e.which == 2 || e.which == 3)  
                return false;  
            } else  if (event.button == 2 || event.button == 3){ 
                 event.cancelBubble = true  
                 event.returnValue = false;  
                 return false;  
            }
        }  
        document.oncontextmenu = nocontextmenu; // for IE5+  
        document.onmousedown = norightclick; // for all others  
    </script>  
</body>
</html>
Copy after login

3.禁止选择文本

<script type="text/javascript">
    var omitformtags=["input", "textarea", "select"];
    omitformtagsomitformtags=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>
Copy after login

4.屏蔽ctrl按键

document.onkeydown=function(){
if(event.ctrlKey)return false;
}
Copy after login


Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!