JS in Firefox key failure solution (not compatible with Firefox solution)

Source: Internet
Author: User

<!doctype HTML Public "-//W3C//DTD XHTML 1.0 transitional//en" "http://www.w3.org/tr/xhtml1/dtd/ Xhtml1-transitional.dtd ""
<meta http-equiv= "Content-type" content= "text/html; charset=gb2312 "/>
<title> Web effects in Firefox key failure Solutions (incompatible Firefox) </title>
<script  language= "Web Effects"
 function mykeypress (evt) {   
 // Compatible IE and Firefox get keyboardevent object    
 evt = (evt)? EVT: ((window.event)? Window.event: "")
&N bsp;//compatible IE and Firefox get KeyboardEvent object's key value    
 var key = Evt.keycode?evt.keycode:evt.which;     
 if (evt.ctrlkey && (key = | | key = =)
 { 
  ;//Both CTRL and enter        
 //do something;   
 
 }
</script>

<body>
IE and Firefox get the keyboard value of different methods, you can understand, Firefox under the Event.which and IE under the event.keycode equivalent. About the difference between each other


<script type= "text/web Effects" >
by Maple Rock @iecn.net
function $ (s) {
return document.getElementById (s) document.getElementById (s): s;
}
function Viewkeyinfo (e) {
var currkey=0,caps tutorial lock=0;
var e=e| | Event
currkey=e.keycode| | e.which| | E.charcode;
Capslock=currkey >=65 && Currkey <=90;
$ ("type"). innerhtml=e[' type '];
$ ("Currkey"). Innerhtml=string.fromcharcode (Currkey);
$ ("decimal"). Innerhtml=currkey;
$ ("KeyCode"). innerhtml=e[' KeyCode '];
$ ("CharCode"). innerhtml=e[' CharCode '];
$ ("caps"). Innerhtml=capslock;
$ ("Shiftkey"). innerhtml=e[' Shiftkey '];
$ ("Ctrlkey"). innerhtml=e[' Ctrlkey '];
$ ("repeat"). innerhtml=e[' Repeat '];
$ ("which"). innerhtml=e[' which '];
}

Document.onkeypress= Viewkeyinfo;
</script>
<p> Please press any key to see the test effect:</p>
Type:<span id= "Type" ></span>

Current Key:<span id= "Currkey" ></span>

Decimal:<span id= "decimal" ></span>

Keycode:<span id= "KeyCode" ></span> <strong> Note: Under FF, KeyCode is always 0</strong>

Which:<span id= "which" ></span> <strong> Note: Under IE, which is always undefined; under opera, KeyCode and charcode have the same values. </strong>

Charcode:<span id= "CharCode" ></span> <strong> Note: Under IE, opera, charcode always for undefined; Under FF, the values of which and charcode are the same </strong>

Capital: <span id= "Caps" ></span>

Altkey:<span id= "Altkey" ></span>

Ctrlkey:<span id= "Ctrlkey" ></span>

Shiftkey:<span id= "Shiftkey" ></span>

Repeat:<span id= "Repeat" ></span>

<style type= "text/css Tutorials" media= "all" >
Body {color: #999; font:normal 14px Tahoma, XXFarEastFont-Arial, Geneva,arial,sans-serif;
span {color: #f00; font-weight:bold;padding:0 5px;}
Strong {color: #090; font-weight:normal;padding:0 5px;}
</style>

Test results:
Under IE:
>> Support KeyCode
>> does not support which and charcode, the values are undefined

Under Firefox:
>> supports keycode, except function keys, the other key values are always 0
>> supports which and charcode, the values are the same

Under Opera:
>> supports KeyCode and which, the values are the same
>> does not support charcode, value is undefined

</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.