Home > Web Front-end > JS Tutorial > body text

How to use keyboard events in jquery

php中世界最好的语言
Release: 2018-04-19 14:44:13
Original
1703 people have browsed it

This time I will show you how to use keyboard events in jquery, and what are the precautions for using keyboard events in jquery, The following is a practical case, let’s take a look.

jQuery has three functions to handle keyboard events. According to the order in which the events occur, they are:

jquery code:

1. keydown();
2. keyup();
3. keypress();

keydown()

The keydown event will be triggered when the keyboard is pressed. You can return false in the bound function to prevent the browser's default event from being triggered.

keyup()

The keyup event will be triggered when the key is released, that is, the event after you press the keyboard and get up.

keypress()

The keypress event is triggered when a key is tapped. We can understand it as pressing and lifting the same key.

Keyboard events can pass a parameter event. In fact, some jQuery event functions can pass such a parameter:

jquery code:

$('input').keydown(function(event){  
    alert(event.keyCode);  
});
Copy after login

In the above code, event.keyCode can help us get what key we pressed. It returns the ASCII code, such as the up, down, left and right keys, which are 38, 40, 37, 39 respectively

If we want to implement ctrl Enter, it is ctrl Enter to submit the form

$(document).keypress(function(e) {  
   if (e.ctrlKey && e.which == 13)
   $("form").submit();  
}) ;
Copy after login

I believe you have mastered the method after reading the case in this article. For more exciting information, please pay attention to other related articles on the PHP Chinese website!

Recommended reading:



The above is the detailed content of How to use keyboard events in jquery. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template