Event. keycode, event. Which

Source: Internet
Author: User

Today, when I went to modify an old system, I solved the compatibility problem that a button triggers on different browsers. It's easy to get away with some information, so don't know why.

First, we will introduce a few simple

Keydown event:

When the browser reads this statement, the keydown () function is called no matter which key is pressed on the keyboard.

 
Function keydown () {} document. onkeydown = keydown;

Read keyboard buttons Under IE

 
Keycode = event. keycode; // string. fromcharcode (event. keycode) // index conversion.

FF reads the keyboard buttons

 
Keycode = E. Which; string. fromcharcode (E. Which); // index conversion.

So you can see that the keyboard buttons are read differently in different browsers,

IE is enent. keycode, and FF is event. Which.

------------------------------------------------------------------------ Gorgeous split line ------------------------------------------------------------------------------------------------------------------

Here, my problem has been solved. You can execute different methods in the browser.

General youth Solutions

It is determined by the appname of navigator.

 
Function keyup (e) {If (navigator. appname = "Microsoft Internet Explorer") {var keycode = event. keycode; var realkey = string. fromcharcode (event. keycode);} else {var keycode = E. which; var realkey = string. fromcharcode (E. which);} alert ("key code:" + keycode + "character:" + realkey);} document. onkeyup = keyup;

Literary Youth Solutions

Function keyup (e) {var currkey = 0, E = E | event; currkey = E. keycode | E. which | E. charcode; var keyname = string. fromcharcode (currkey); alert ("key code:" + currkey + "character:" + keyname);} document. onkeyup = keyup;

Explain

E = E | event

This sentence in JSCodeIf the Hidden variable e exists in Firefox or opera, E | event returns E. If the Hidden variable e does not exist in IE, event is returned.

Currkey = E. keycode | E. Which | E. charcode;

This statement is intended to be compatible with the key code attribute of the browser's key event object (see section 3). For example, in IE, only the keycode attribute exists, while in Firefox, the which and charcode attributes exist, opera has the keycode and which attributes.

------------------------------------------------------------------- I split it again ----------------------------------------------------------------------


The problem has been solved. The problem is very basic. I tried it with jquery.

I found a piece of code on the Internet.

 
<Script language = "JavaScript"> $ (function () {$ ("# input1 "). keydown (function (event) {$ ("# div1" ).html ("key:" + event. keycode) ;}); </SCRIPT>

 



 

 

 

 

 

 

 

 

 

This is the effect in IE.

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.