一聚教程网:一个值得你收藏的教程网站

最新下载

热门教程

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

时间:2010-07-14 编辑:简简单单 来源:一聚教程网





<a target="_blank" href="/js_a/js.html">网页特效</a> 在firefox按键失效解决办法(不兼容firefox处理办法)
网页特效">
 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;   
 }
 }


ie和firefox获取键盘值的方法不同,可以理解,firefox下的event.which与ie下的event.keycode相当。关于彼此不同



请按下任意键看测试效果:


type:
 
当前key:
 
decimal:
 
keycode: 注:在ff下,keycode始终为0
 
which: 注:在ie下,which始终为undefined ; 在opera下,keycode和charcode二者的值相同
 
charcode: 注:在ie、opera下,charcode始终为undefined ; 在ff下,which和charcode二者的值相同
 
大写:
 
altkey:
 
ctrlkey:
 
shiftkey:
 
repeat:
 

测试结果:
在ie下:
>> 支持keycode
>> 不支持which和charcode,二者值为 undefined

在firefox下:
>> 支持keycode,除功能键外,其他键值始终为 0
>> 支持which和charcode,二者的值相同

在opera下:
>> 支持keycode和which,二者的值相同
>> 不支持charcode,值为 undefined


热门栏目