参考链接:http://blog.csdn.net/zhouziyu2011/article/details/53978293
- <input type="text" id="text">
- <script>
- document.getElementById("text").onkeypress = function(e) {
- e = e || window.event;
- key = e.keyCode || e.which || e.charCode;
- alert("按键码: " + key + " 字符: " + String.fromCharCode(key));
- };
- </script>
说明:IE只有keyCode属性,FireFox中有which和charCode属性,Opera中有keyCode和which属性,Chrome中有keyCode、which和charCode属性。