1. 程式人生 > >js onkeyup 相容火狐和IE

js onkeyup 相容火狐和IE

// 方法1
function keyUp(e) {
	if(navigator.appName == "Microsoft Internet Explorer"){
		var keycode = event.keyCode;
		var realkey = String.fromCharCode(event.keyCode);
	} else {
		var keycode = e.which;
	var realkey = String.fromCharCode(e.which);
	}
	alert("keyCode: " + keycode + " word: " + realkey);
}
// 方法2
function keyUp1(e) {
	var currKey=0,e=e||event;
	currKey=e.keyCode||e.which||e.charCode;
	var keyName = String.fromCharCode(currKey);
	alert("keyCode: " + currKey + " word: " + keyName);
}

//document.onkeyup = keyUp;
document.onkeyup = keyUp1;