简体   繁体   English

尝试找出jQuery中是否有错误或我在做什么

[英]Trying to figure out if there is a bug in jQuery or if it's something I'm doing

$(document).keydown(function (event)
    {
    alert(event.which);
    });

For the semicolon key, ; 对于分号键; , this gives 59 in Firefox and 186 in Chrome. ,在Firefox中为59,在Chrome中为186。 However, from the jQuery reference page for the keydown event, it says 但是,在keydown事件的jQuery参考页面中,它表示

"While browsers use differing properties to store this information, jQuery normalizes the .which property so you can reliably use it to retrieve the key code. This code corresponds to a key on the keyboard, including codes for special keys such as arrows." “虽然浏览器使用不同的属性来存储此信息,但jQuery规范化了.that属性,因此您可以可靠地使用它来检索键代码。此代码与键盘上的键相对应,包括特殊键(如箭头)的代码。”

Am I missing something? 我想念什么吗?

The which property is a "one stop shop" for which key was pressed, allowing you to ignore the differences between the keyCode and charCode properties. which属性是按下了哪个键的“一站式服务”,您可以忽略keyCodecharCode属性之间的差异。 That is the "normalization" that jQuery provides. 那就是jQuery提供的“规范化”。

The difference in the value of which comes down to a difference between the way the various browsers supply the information - so you'll have to write code to handle the different values that come back. 在价值份额的差额which归结为不同的浏览器提供的信息的方式之间的差异-所以你必须编写代码来处理回来的不同的值。 There are a few references to this behavior online. 在线有一些对此行为的引用

A quick Google search says you will simply have to test for both. 快速的Google搜索表明,您只需对两者进行测试。 This is a consistent inconsistency with Firefox. 这与Firefox始终存在不一致之处。

我不了解jQuery,但建议您坚持按keypress事件来键入键,而仅对诸如箭头之类的特殊键使用keydown事件。

Here is the entirety of the "normalization" that jQuery does: 这是jQuery所做的“规范化”的全部内容:

if ( event.which == null ) {
    event.which = original.charCode != null ? original.charCode : original.keyCode;
}

Looks like it just gets keyCode if charCode doesn't exist. 如果charCode不存在,它看起来只会得到keyCode And charCode is only used if event.which doesn't already exist. 并且charCode仅在event.which不存在的情况下使用。 It doesn't change the numbers around to make them consistent. 它不会更改周围的数字以使它们保持一致。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM