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

后端 未结 1 1487
情话喂你
情话喂你 2020-12-20 19:31

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

Clicking - (dash) in Firefox says that event.which is 173, while doing th

相关标签:
1条回答
  • 2020-12-20 20:20

    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.

    0 讨论(0)
提交回复
热议问题