日期:2014-05-16  浏览次数:20449 次

js禁用回退键[backspace键]浏览历史跳转的解决办法
window.onload=function(){ 
  
    document.getElementsByTagName("body")[0].onkeydown =function(){ 
         
        //获取事件对象 
        var elem = event.relatedTarget || event.srcElement || event.target ||event.currentTarget;  
         
        if(event.keyCode==8){//判断按键为backSpace键 
         
                //获取按键按下时光标做指向的element 
                var elem = event.srcElement || event.currentTarget;  
                 
                //判断是否需要阻止按下键盘的事件默认传递 
                var name = elem.tagName; 
                 
                if(name!='INPUT' && name!='TEXTAREA'){ 
                    return _stopIt(event); 
                } 
                var type_e = elem.type.toUpperCase(); 
                if(name=='INPUT' && (type_e!='TEXT' && type_e!='TEXTAREA' && type_e!='PASSWORD' && type_e!='FILE')){ 
                        return _stopIt(event); 
                } 
                if(name=='INPUT' && (elem.readOnly==true || elem.disabled ==true)){ 
                        return _stopIt(event); 
                } 
            } 
        } 
    } 
function _stopIt(e){ 
        if(e.returnValue){ 
            e.returnValue = false ; 
        } 
        if(e.preventDefault ){ 
            e.preventDefault(); 
        }                
 
        return false;