This article analyzes keyboard events in js. Share it for your reference. The specific analysis is as follows:
The effect of this example:
Press any key on the keyboard to pop up the corresponding ASCII code, which is compatible with ie, chrome and firefox.
But there are still many problems:
(1) In IE and Chrome, some keys have no effect, such as top, bottom, left, right, etc.;
(2) The right arrow key in firefox and the single quote key are both 39.
The specific code is as follows:
Copy the code as follows: <html>
<head>
<script type="text/javascript">
window.onload = function(){
var bd = document.getElementsByTagName('body')[0];
bd.onkeypress = function(ev){
ev = ev || window.event;//ie does not support function parameter ev
alert(ev.keyCode || ev.which);// Firefox does not support keyCode
}
}
</script>
<style type="text/css">
#par{width:300px;height:200px;background:gray;}
#son{width:200px;height:100px;background:green;}
</style>
</head>
<body>
<div id="par">
<div id="son"></div>
</div>
</body>
</html>
I hope this article will be helpful to everyone's JavaScript programming.