Home > Article > Web Front-end > How to use keyboard events in jquery
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); });
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(); }) ;
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!