当前位置: 代码迷 >> 综合 >> 屏蔽浏览器界面Backspace按钮单击事件
  详细解决方案

屏蔽浏览器界面Backspace按钮单击事件

热度:0   发布时间:2024-01-15 23:29:09.0

//屏蔽IE内核的浏览器界面的Backspace按钮单击事件

document.onkeydown =function (e) {  

                 var code,type;     
                 if (!e){ var e = window.event;}     
                 if (e.keyCode){ code = e.keyCode;}  
                 else if (e.which){ code = e.which;}  
                 type = event.srcElement.type;  
                 if ((code == 8)  
                   && ((type != "text" && type != "textarea"   
                   &&type != "password") ||  event.srcElement.readOnly == true)) {  
                       event.keyCode = 0;  
                       event.returnValue = false;  
                 }  
                 return true;  

            } 

//屏蔽Firefox等浏览器界面的Backspace按钮单击事件

    if(typeof window.event == 'undefined') {  
                 document.onkeypress = function(e) {  
                    var type = e.target.localName.toLowerCase();  
                    var code = e.keyCode;  
                    if ((code != 8 && code != 13) ||  
                        (type == 'input' && code != 13 ) ||  
                        (type == 'textarea') ||  
                        (type == 'submit' && code == 13)) {  
                        return true;  
                    } else {  
                        return false ;  
                    }  
                }  
            }