jquery uses the Event.which method to get code for keyboard input values _jquery

Source: Internet
Author: User
Instance
Show which key was pressed:
$ ("input"). KeyDown (function (event) {
$ ("div"). html ("Key:" + Event.which);
});
Give it a shot yourself.
Definitions and usage
The Which property indicates which key or button was pressed.
Grammar
Event.which parameter Description
Event required. Specify the events to check. This event parameter comes from the event-binding function.
jquery discards the standard button attribute using which, which is a bit confusing.
Which is introduced by Firefox, IE does not support. Which is meant to get the keyboard's key value (KeyCode).
The which in jquery can be either the key value of the keyboard or the key value of the mouse.
That is, when the user to determine which button the keyboard can be used which, when the user to determine which button to press the mouse can also use which. It's dual-use.
Source:
Copy Code code as follows:

Add which for key events
if (Event.which = = null && (event.charcode!= null | | event.keycode!= null)) {
Event.which = Event.charcode!= null? Event.charCode:event.keyCode;
}
Add which for click:1 = = left; 2 = = middle; 3 = = Right
Note:button isn't normalized, so don ' t use it
if (!event.which && event.button!== undefined) {
Event.which = (Event.button & 1 1: (Event.button & 2 3: (Event.button & 4 2:0));
}

The standard button uses 0,1,2 to represent the left, middle, and right keys of the mouse. The Which of jquery is used with 1,2,3.
It's also a bit of a bummer that the jquery document Event.which does not mention that which can represent the mouse button value, only refers to the keyboard key value.
The comments in the source code are also misleading.
Add which for click:1 = = left; 2 = = middle; 3 = = Right
Note that here is the click, it is easy to let people use the click event, but actually click the event to get is wrong.
Let's try this with the Click event:
Copy Code code as follows:

<! DOCTYPE html>
<meta charset= "Utf-8"/>
<title></title>
<script src= "Http://demo.jb51.net/jslib/jquery/jquery-1.6.1.js" ></script>
<script type= "Text/css" >
$ (document). Click (function (e) {
alert (E.which);
})
</script>
<body>
</body>

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.