javascript event.keycode 与 event.which

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/12172646/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-26 15:29:35  来源:igfitidea点击:

event.keycode vs event.which

javascriptjqueryjavascript-eventsevent-handling

提问by codepuppy

I fell foul of a Firefox keydownbehavior in that pressing the enter key (indeed any key) without having focus on a specific field will NOT trigger a keydownevent it will only trigger a keypressevent.

我违反了 Firefox keydown行为,因为在没有关注特定字段的情况下按下 Enter 键(实际上是任何键)不会触发keydown事件,它只会触发keypress事件。

This could be very confusing as the keydownand keyupevent use JavaScript key codes whereas keypressuses ASCII codes. Fortunately 13 (enter/return) is common to both.

这可能会非常令人困惑,因为keydownkeyup事件使用 JavaScript 键代码,而keypress使用 ASCII 代码。幸运的是,13(输入/返回)对两者都是通用的。

Is there any known reason why FF using keypressin this circumstance? What is the benefit?

在这种情况下,FF 使用按键是否有任何已知原因?有什么好处?

Once this was established IE8 threw up a silly in that it does not permit preventDefaultdemanding instead returnValue = falsethe following snippet from another SO post has proved very useful:

一旦确定,IE8 就抛出了一个愚蠢的问题,因为它不允许preventDefault要求取而代之的returnValue = false是来自另一篇 SO 帖子的以下片段被证明非常有用:

event.preventDefault ? event.preventDefault() : event.returnValue = false;

During the search to resolve these issues I have been consistently confused by event.keycodevs event.which. Namely am I doing wrong using a switch statement similar to:

在寻找解决这些问题的过程中,我一直event.keycode对 vs感到困惑event.which。即我是否使用类似于以下的 switch 语句做错了:

$("#class_Name").bind("keydown", function(event){
    // do not test input if field controls used
    switch(event.which){
       case 13:
       //enter key 
       event.preventDefault ? event.preventDefault() : event.returnValue = false;
       break;
     }


Is the following better, if so why?


以下是否更好,如果是,为什么?

$("body").keypress(function(event){
     // stop inadvertant form submission
     if (event.keycode == "13"){
       event.preventDefault ? event.preventDefault() : event.returnValue = false;
     }
});


I would just like to know so that I know which is best to apply.


我只想知道这样我就知道哪个最适合申请。

Many thanks.

非常感谢。

采纳答案by sQVe

Some browsers use keyCodeand others use which. But with jQuery this is normalizedso you don't have to think about that. You can just choose the one you prefer.

一些浏览器使用,keyCode而另一些浏览器使用which. 但是使用 jQuery,这是标准化的,因此您不必考虑这一点。您可以选择您喜欢的一种。

回答by Cees Timmerman

According to this commentjQuery can be unreliable and this pagesays:

根据此评论jQuery 可能不可靠,此页面说:

event.whichis undefined in IE<9 on keydownand keyup.

event.which在 IE<9keydown和上未定义keyup

event.keyCodeis 0 in Gecko (Seamonkey, Firefox) on keypressfor keys that return a character.

event.keyCodekeypress对于返回字符的键,在 Gecko (Seamonkey, Firefox) on 中为0 。

event.charCodeis only supported on keydown and keyup by Internet Explorer (Mac).

event.charCode仅在 Internet Explorer (Mac) 上支持 keydown 和 keyup。

Try it on JSFiddle

在 JSFiddle 上试试