JavaScript键处理和浏览器兼容性 [英] JavaScript key handling and browser compatibility

查看:91
本文介绍了JavaScript键处理和浏览器兼容性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在处理java脚本中的键处理。我已经做了一些研究,我想知道我是否对键处理有正确的理解。



KeyDown / KeyUp事件
IE7 +和Firefox 3.5+支持key down和key up事件我没有检查早期版本的浏览器,但我想他们也支持这些事件。


$ b $



CharCode


$ b

charCode值在keypress上可用。大多数键将具有表示实际值的字符码。一些键将不具有与它们相关联的字符码。例如。退格,删除,箭头键。



我可以说,在按键上的charcode会和keycode一样。



strong>活动顺序




  • KeyDown

  • KeyPress

  • KeyUp



这个顺序会因浏览器而异吗?例如我有两个函数。首先绑定到KeyDown事件,第二个绑定到KeyPress事件。调用KeyPress事件意味着KeyDown事件也将被调用,当我只想要其中一个事件工作。



最后,我一直在考虑使用不同的键处理例程取决于版本浏览器。例如:




  • 检查浏览器版本

  • 根据浏览器版本获取密钥处理例程



这将引入额外的代码,但应该简化维护。此外,在将来,当我想为不同的浏览器提供支持时,我可以简单地添加另一个例程,并且不会影响现有的字符处理例程。



到目前为止,我一直在阅读 http://www.quirksmode.org



/help.dottoro.com/ljlkwans.phprel =nofollow noreferrer> onkeydown event
onkeypress event
keyCode属性
charCode属性
which property


I'm working on key handling in java script. I have done some research and I'd like to know whether I have a correct understanding of key handling.

KeyDown/KeyUp Event The key down and key up events are supported by IE7+ and Firefox 3.5+ I didn't check the earlier versions of the browsers, but I guess that they also support these events.

Is it correct to say that each key on the keyboard will always have a keycode.

CharCode

CharCode value is available on the keypress.Majority of the keys will have the charcodes that represent the actual value. Some keys won't have a charcode associated with them. E.g. backspace, delete, arrow keys.

Am I correct to say that on the keypress the charcode will be the same as the keycode?

Order of the events

  • KeyDown
  • KeyPress
  • KeyUp

Does this order differ from browser to browser? For example I have two functions. First is bound to KeyDown event, second is bound to the KeyPress event. Calling a KeyPress event means that the KeyDown event will also be called, when I want only one of these events to work.

Finally I have been considering on using different key handling routines depending on the version browser. For example:

  • Check browser version
  • Get key handling routine depending on the browser version

This will introduce additional code, but should simplify the maintenance. Also, in the future, when I want to provide a support for a different browser, I can simply add another routine and it won't affect existing character handling routine.

So far I have been reading http://www.quirksmode.org

解决方案

See the following pages, they will answer your quetions:

onkeydown event, onkeypress event, keyCode property, charCode property, which property

这篇关于JavaScript键处理和浏览器兼容性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆