js中的按键事件

参考链接:http://blog.csdn.net/zhouziyu2011/article/details/53978293

  1. <input type="text" id="text">  
  2. <script>  
  3.     document.getElementById("text").onkeypress = function(e) {  
  4.         e = e || window.event;  
  5.         key = e.keyCode || e.which || e.charCode;  
  6.         alert("按键码: " + key + " 字符: " + String.fromCharCode(key));       
  7.     };  
  8. </script>  
 

说明:IE只有keyCode属性,FireFox中有which和charCode属性,Opera中有keyCode和which属性,Chrome中有keyCode、which和charCode属性。

原文地址:https://www.cnblogs.com/zhangzs000/p/6242675.html