javascript e.keyCode 和 e.which 有什么区别?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/19249351/
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-27 14:53:20  来源:igfitidea点击:

what is the difference between e.keyCode and e.which?

javascriptjquery

提问by Rituraj ratan

I am working with jQueryin this when I try e.keyCodeFor enter key like below

当我尝试像下面这样的输入键时,我正在使用jQuerye.keyCode

    if (e.keyCode === 13) {
// my code
    }

then it works with ie7 and all major browsers but not in ie10.

然后它适用于 ie7 和所有主要浏览器,但不适用于 ie10。

But when I use e.whichit runs in all major browsers.

但是当我使用e.which它时,它会在所有主要浏览器中运行。

What is the difference between e.keyCodeand e.which?

e.keyCode和 和有什么不一样e.which

回答by mucio

The event.which property normalizes event.keyCode and event.charCode. It is recommended to watch event.which for keyboard key input.

event.which 属性规范了 event.keyCode 和 event.charCode。建议观看 event.which 进行键盘按键输入。

As per JQuery documentation

根据JQuery 文档

keyCode is standard JavaScript and of course not implemented in the same way by all browsers.

keyCode 是标准的 JavaScript,当然并非所有浏览器都以相同的方式实现。