zoukankan      html  css  js  c++  java
  • JS禁用浏览器退格键、禁止右键、禁止全选、复制、粘贴

    一、禁用浏览器退格键

    摘抄自:https://www.cnblogs.com/wanggd/p/3164536.html

    我们在真实的项目开发中经常会使用JS 对键盘上的一些按键进行禁用,常见的比如说退格键(backspace/ 后退键),我在一个项目中就遇到过在页面编辑的时候禁用掉退格键,因为退格键会发生页面后退,这样编辑的内容都会失去了,非常的恶心人。ok ,废话少说,直接上代码。

      <script type="text/javascript">
            //处理键盘事件 禁止后退键(Backspace)密码或单行、多行文本框除外
            function forbidBackSpace(e) {
                var ev = e || window.event; //获取event对象 
                var obj = ev.target || ev.srcElement; //获取事件源 
                var t = obj.type || obj.getAttribute('type'); //获取事件源类型 
                //获取作为判断条件的事件类型 
                var vReadOnly = obj.readOnly;
                var vDisabled = obj.disabled;
                //处理undefined值情况 
                vReadOnly = (vReadOnly == undefined) ? false : vReadOnly;
                vDisabled = (vDisabled == undefined) ? true : vDisabled;
                //当敲Backspace键时,事件源类型为密码或单行、多行文本的, 
                //并且readOnly属性为true或disabled属性为true的,则退格键失效 
                var flag1 = ev.keyCode == 8 && (t == "password" || t == "text" || t == "textarea") && (vReadOnly == true || vDisabled == true);
                //当敲Backspace键时,事件源类型非密码或单行、多行文本的,则退格键失效 
                var flag2 = ev.keyCode == 8 && t != "password" && t != "text" && t != "textarea";
                //判断 
                if (flag2 || flag1) return false;
            }
            //禁止后退键 作用于Firefox、Opera
            document.onkeypress = forbidBackSpace;
            //禁止后退键  作用于IE、Chrome
            document.onkeydown = forbidBackSpace;
    </script>

    使用方法:把上面的js代码放到<head></head>之间就ok了

    chorme支持

      支持WinXP的最高版本是49.0.2623.112。下载:http://www.newasp.net/soft/71467.html,点下载压缩包内的40.0.2214.91_chrome_installer.exe会自动下载安装,安装后的程序文件日期是2016-4-6。2016-04-14Google发布的Chrome 50不再支持XP。

    二、禁止鼠标右键、全选、复制、粘贴


    原文:https://www.cnblogs.com/happiness-mumu/p/6269465.html

    禁用右键菜单

    js代码:

    document.oncontextmenu = function(){
        event.returnValue = false;
    }
    // 或者直接返回整个事件
    document.oncontextmenu = function(){
        return false;
    }

    禁用网页上选取的内容

     
    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>中加入如下的代码,这样鼠标的左右键都失效了.


    禁止网页另存为


    在<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()"></body>


  • 相关阅读:
    并列显示
    vertical-align,text-align 和 align的区别
    实现水平垂直居中
    overflow属性
    float属性
    table 标签
    idea中修改默认maven
    使用host的方式来破解idea
    mysql分区
    mysql数据库设计规范
  • 原文地址:https://www.cnblogs.com/xcsn/p/9293054.html
Copyright © 2011-2022 走看看