Native js implements cross-browser acquisition of mouse key value _ javascript skills

Source: Internet
Author: User
E. buttonW3C is used to obtain the mouse Key 0. The left key 1 indicates the middle key 2 indicates the right click, while the IE browser 1 indicates the left Key 4 indicates the middle 2 indicates the right click. The IE browser here is mainly a browser under IE8, if you are interested, refer The Code is as follows:


Document. onmousedown = function (e ){
Alert (getButton (e) // e. button W3C is used to obtain the mouse Key 0. The left key 1 indicates the middle key 2 indicates the right click, while the IE browser 1 indicates the left Key 4 indicates the middle 2 indicates the right click. Here, the IE browser is mainly a browser under IE8.
};
Function getButton (e ){
/*
1. Both IE and Chrome support the window. event attribute.
2. Chrome also supports W3C
3. If both W3C and IE support, W3C has been standardized.
*/
If (e) {// Chrome is based on W3C as the first judgment.
Return e. button;
} Else if (window. event ){
Switch (window. event. button ){
Case 1: return 0; // return the value of the left mouse button
Case 4: return 1; // return the value of the middle mouse key.
Case 2: return 2; // return the right-click Value
Case 0: return 2; // the value of the right mouse button returned is mainly 360. the browser returns 0 in IE, which indicates the value returned when the mouse key is not pressed.
};
};
};

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.