Jquery uses the event. which method to obtain the code of the keyboard input value

Source: Internet
Author: User

Instance
Show which key is pressed:
$ ("Input"). keydown (function (event ){
$ ("Div" ).html ("Key:" + event. which );
});
Try it yourself
Definition and usage
The which attribute indicates which key or button is pressed.
Syntax
Event. which parameter description
Event is required. Specifies the event to be checked. 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, which is not supported by IE. Which is intended to get the keyCode of the keyboard ).
In jQuery, which can be the key value of the keyboard or the key value of the mouse.
That is, when you determine which key the user presses the keyboard, you can use which to determine which key the user presses the mouse. It is used in one fell swoop.
Source code:
Copy codeThe Code is 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 is not 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 indicate the left, center, and right-click of the mouse. JQuery's which uses 1, 2, 3.
Another unpleasant thing is that the event. which in the jQuery document does not mention that which can represent the mouse key value, but only the keyboard key value.
Comments in the source code are also misleading.
// Add which for click: 1 === left; 2 === middle; 3 === right
Note that click is used, which makes it easy to use the click event. However, it is incorrect to obtain the click event.
Next, we will try the click event:
Copy codeThe Code is as follows:
<! DOCTYPE html>
<Html>
<Head>
<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>
</Head>
<Body>
</Body>
</Html>

Related Article

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.