Home >Web Front-end >JS Tutorial >Introduction to javascript/jquery keyboard events

Introduction to javascript/jquery keyboard events

黄舟
黄舟Original
2016-12-16 10:52:461465browse

1. The first thing you need to know is:
1. keydown()
keydown event will be triggered when the keyboard is pressed.
2. keyup()
keyup event will be triggered when the key is released, that is, after you press the keyboard and get up The event
3. keyPRess()
keypress event will be triggered when the key is tapped. We can understand it as pressing and lifting the same key

Second, get the corresponding asciII code on the keyboard:

$(document). keydown(function(event){
alert(event.keyCode);
});

$tips: In the above example, event.keyCode can help us get what keys on the keyboard we pressed. What he returns is the ASCII code, for example, the up, down, left and right keys are 38, 40, 37, 39 respectively;

3. Example (when the left and right keys on the keyboard are pressed)
The code is as follows:

$(document) .keydown(function(event){
//Judge when event.keyCode is 37 (i.e. left side key), execute function to_left(); //Judge when event.keyCode is 39 (i.e. right side key), execute Function to_right();
if(event.keyCode == 37){
                } else if (event.keyCode == 39) {                                        else if (event.keyCode == 38){
                                                                                          ){ $(".abc").CSS( {'left':'-=10'});}function to_right(){ $(".abc").css({'left':'+=10'});}function to_top(){$( ".abc").css({'top':'-=10'});}function to_bottom(){$(".abc").css({'top':'+=10'}); }




The above is the introduction of javascript/jquery keyboard events. For more related articles, please pay attention to the PHP Chinese website (www.php.cn)!



Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn