为什么使用jQuery的event.Firefox和Chrome会产生不同的结果? [英] Why jQuery's event.which gives different results in Firefox and Chrome?

查看:117
本文介绍了为什么使用jQuery的event.Firefox和Chrome会产生不同的结果?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

看看此实时演示(来自jQuery的网站).

Have a look at this live demo (from jQuery's site).

在Firefox中单击-(破折号)表示event.which173,而在Chrome中执行相同操作会生成189.

Clicking - (dash) in Firefox says that event.which is 173, while doing the same in Chrome produces 189.

此jQuery页面指出,event.which应该针对跨浏览器进行规范化一致性.但是,看来这是不正确的.

This jQuery page says that event.which should be normalized for cross browser consistency. But, it looks like this is not true.

为什么会有这种不一致?

Why is this inconsistency?

推荐答案

此jQuery页面上说明了该事件.应对其进行标准化以实现跨浏览器的一致性.但是,看来这是不正确的.

This jQuery page says that event.which should be normalized for cross browser consistency. But, it looks like this is not true.

jQuery规范化属性 name (例如,始终为which,而不是whichkeyCode取决于浏览器),但不会规范化 value 属性,它会变得更加复杂.

jQuery normalizes the property name (e.g., always which, rather than which or keyCode depending on browser), but not the value of the property, which would be dramatically more complex.

keydown/keyup获取的键的值不仅会因浏览器而异,也会因键盘布局而异. JavaScript疯狂:键盘事件 页上有很多细节.扬·沃尔特(Jan Wolter)撰写.除其他外,您可以在该页面上看到该密钥,Firefox将给您109,IE(显然是Chrome)将给您189,Opera显然曾经给您提供45(但是在我在Linux上的测试中,现在加上109).

The value for the key you get from keydown / keyup will vary not only by browser, but by keyboard layout. There are lots of gory details on the JavaScript Madness: Keyboard Events page by Jan Wolter. Amongst other things, you can see on that page that for that key, Firefox will give you 109, IE (and apparently Chrome) will give you 189, and Opera apparently used to go with 45 (but in my tests on Linux, they now go with 109).

对于可打印的击键(如-),最好使用keypress事件,该事件为您提供生成的字符.

For printable keystrokes (like -), you're better off with the keypress event, which gives you the resulting character.

这篇关于为什么使用jQuery的event.Firefox和Chrome会产生不同的结果?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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