解决IE8和火狐等浏览器的backspace键退回的问题

好久没写点东西了,今天写个解决IE8和火狐等浏览器的backspace键退回的问题

IE如下:
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;
		}


火狐及其他如下:
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 ;
			    }
			}
		}

你可能感兴趣的:(浏览器)