Why jQuery's event.which gives different results in Firefox and Chrome?

梦想的初衷 提交于 2019-12-18 08:22:32

问题


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

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

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?


回答1:


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

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.

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).

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



来源:https://stackoverflow.com/questions/18177818/why-jquerys-event-which-gives-different-results-in-firefox-and-chrome

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!