我想找出在纯JavaScript中以跨浏览器兼容的方式按下了哪些字符键。
“清除” JavaScript:
<script type="text/javascript"> function myKeyPress(e){ var keynum; if(window.event) { // IE keynum = e.keyCode; } else if(e.which){ // Netscape/Firefox/Opera keynum = e.which; } alert(String.fromCharCode(keynum)); } </script> <form> <input type="text" onkeypress="return myKeyPress(event)" /> </form>
jQuery的:
$(document).keypress(function(event){ alert(String.fromCharCode(event.which)); });