var ie;
if (document.all) ie = true; else ie = false; //判断是否IE
document.onkeydown = KeyPress; //设置键盘事件函数
function KeyPress(){
var key;
if (ie) key = event.keyCode; //ie获取键盘值
else key = KeyPress.arguments[0].keyCode; //其它浏览器获取键盘值
console.log(key); //输出对应的键盘值
}
例子:
<!DOCTYPE html><html><head><meta charset="utf-8"><title>key press</title></head><body>F12 查看键盘对应的数值</body><script type="text/javascript">var ie;if (document.all) ie = true; else ie = false; //判断是否IEdocument.onkeydown = KeyPress; //设置键盘事件函数function KeyPress() {var key;if (ie) key = event.keyCode; //ie获取键盘值else key = KeyPress.arguments[0].keyCode; //其它浏览器获取键盘值console.log(key); //输出对应的键盘值}</script></html>
enter 13