This article mainly introduces the keyboard events in js, and analyzes the operation skills of js in response to keyboard events in the form of a relatively simple example. Friends in need can refer to the following
This article analyzes the keyboard in js with an example event. Share it with everyone 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, compatible with IE, chrome and firefox.
But there are still many problems:
(1) In IE and Chrome, some keys have no effect, such as up, down, left, right, etc.;
(2) In Firefox The right arrow key, and the single quote key, are both 39.
The specific code is as follows:
The code is 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不支持function参数ev alert(ev.keyCode || ev.which);//火狐不支持keyCode } } </script> <style type="text/css"> #par{width:300px;height:200px;background:gray;} #son{width:200px;height:100px;background:green;} </style> </head> <body> <p id="par"> <p id="son"></p> </p> </body> </html>
The above is the detailed content of A simple explanation of keyboard events in js. For more information, please follow other related articles on the PHP Chinese website!