首页 > web前端 > js教程 > js相关禁止

js相关禁止

高洛峰
发布: 2016-10-15 17:47:10
原创
1281 人浏览过

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

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

1

2

3

4

<script type="text/javascript">

    document.oncontextmenu=new Function("event.returnValue=false;");

    document.onselectstart=new Function("event.returnValue=false;");

</script>

登录后复制

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

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

<!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>

登录后复制

3.禁止选择文本

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

<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>

登录后复制

4.屏蔽ctrl按键

1

2

3

document.onkeydown=function(){

if(event.ctrlKey)return false;

}

登录后复制


相关标签:
本站声明
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板