百分百源码网-让建站变得如此简单! 登录 注册 签到领金币!

主页 | 如何升级VIP | TAG标签

当前位置: 主页>网站教程>JS教程> js 在Firefox按键失效解决办法(不兼容firefox处理办法)
分享文章到:

js 在Firefox按键失效解决办法(不兼容firefox处理办法)

发布时间:01/15 来源: 浏览: 关键词:

<!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>

打赏

打赏

取消

感谢您的支持,我会继续努力的!

扫码支持
扫码打赏,你说多少就多少

打开支付宝扫一扫,即可进行扫码打赏哦

百分百源码网 建议打赏1~10元,土豪随意,感谢您的阅读!

共有1人阅读,期待你的评论!发表评论
昵称: 网址: 验证码: 点击我更换图片
最新评论

本文标签

广告赞助

能出一分力是一分吧!

订阅获得更多模板

本文标签

广告赞助

订阅获得更多模板