<!doctype html public "-//w3c//dtd xhtml 1.0 transitional//en" "http://www.w3.org/tr/xhtml1/dtd/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="content-type" content="text/html; charset=gb2312" />
<title>网页特效 在firefox按键失效解决办法(不兼容firefox处理办法)</title>
<script language="网页特效">
function mykeypress(evt){
//兼容ie和firefox获得keyboardevent对象
evt = (evt) ? evt : ((window.event) ? window.event : "")
//兼容ie和firefox获得keyboardevent对象的键值
var key = evt.keycode?evt.keycode:evt.which;
if(evt.ctrlkey && (key == 13 || key == 10))
{
//同时按下了ctrl和回车键
//do something;
}
}
</script>
</head>
<body>
ie和firefox获取键盘值的方法不同,可以理解,firefox下的event.which与ie下的event.keycode相当。关于彼此不同
<script type="text/网页特效">
//by 枫岩@iecn.net
function $(s){
return document.getelementbyid(s)?document.getelementbyid(s):s;
}
function viewkeyinfo(e){
var currkey=0,caps教程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>请按下任意键看测试效果:</p>
type:<span id="type"></span>
当前key:<span id="currkey"></span>
decimal:<span id="decimal"></span>
keycode:<span id="keycode"></span> <strong>注:在ff下,keycode始终为0</strong>
which:<span id="which"></span> <strong>注:在ie下,which始终为undefined ; 在opera下,keycode和charcode二者的值相同</strong>
charcode:<span id="charcode"></span> <strong>注:在ie、opera下,charcode始终为undefined ; 在ff下,which和charcode二者的值相同</strong>
大写:<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教程" media="all">
body {color:#999;font:normal 14px tahoma,宋体,geneva,arial,sans-serif;}
span {color:#f00;font-weight:bold;padding:0 5px;}
strong {color:#090;font-weight:normal;padding:0 5px;}
</style>
测试结果:
在ie下:
>> 支持keycode
>> 不支持which和charcode,二者值为 undefined
在firefox下:
>> 支持keycode,除功能键外,其他键值始终为 0
>> 支持which和charcode,二者的值相同
在opera下:
>> 支持keycode和which,二者的值相同
>> 不支持charcode,值为 undefined
</body>
</html>