javascript禁制后退键,实例代码

作者: 前端技术  发布:2019-09-03

复制代码 代码如下:

function banBackSpace(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 = banBackSpace; 
            //禁止退格键 效用于IE、Chrome 
            document.onkeydown = banBackSpace;   

function banBackSpace(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 

摘自  tmaic的专栏 

            } 
            //禁止退格键 功能于Firefox、Opera 
            document.onkeypress = banBackSpace; 
            //禁止退格键 功效于IE、Chrome 
            document.onkeydown = banBackSpace; 

banBackSpace(e) { var ev = e || window.event;//获取event对象 var obj = ev.target || ev.srcElement;//获取事件源 var t = obj.type || obj.getAttribute(type);//获取事...

代码如下: function banBackSpace(e) { var ev = e || window.event;//获取event对象 var obj = ev.target || ev.srcElement;//获取事件源 var t = obj.type || obj....

本文由王中王开奖结果发布于前端技术,转载请注明出处:javascript禁制后退键,实例代码

关键词: