Home  >  Article  >  Web Front-end  >  Introduction to jquery keyboard events_jquery

Introduction to jquery keyboard events_jquery

WBOY
WBOYOriginal
2016-05-16 18:11:081162browse
1. The first thing you need to know is:
1. keydown()
The keydown event will be triggered when the keyboard is pressed.
2. keyup()
The keyup event will be triggered when the keyboard is pressed. It is triggered when the key is released, that is, the event after you press the keyboard and lift it up
3. keypress()
The keypress event will be triggered when the key is tapped. We can understand it as pressing and lifting the same key


2. Obtain the corresponding ascII code on the keyboard:
$(document).keydown(function(event){
alert(event.keyCode);
});

$tips: In the above example, event.keyCode can help us get what keys we pressed on the keyboard. It returns the ascII code, such as the up, down, left and right keys, respectively. It is 38, 40, 37, 39;

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

$(document).keydown(function(event){
//Judge when event.keyCode is 37 ( That is, the left side key), execute the function to_left();
//Judge when event.keyCode is 39 (that is, the right side key), execute the function to_right();
if(event.keyCode == 37) {
to_left();
}else if (event.keyCode == 39){
to_right();
}
});


4. TIPS:
This example is often used when browsing electronic photo albums. . .
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