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
what is the difference between e.keyCode and e.which?
提问by Rituraj ratan
I am working with jQueryin this when I try e.keyCode
For 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.which
it runs in all major browsers.
但是当我使用e.which
它时,它会在所有主要浏览器中运行。
What is the difference between e.keyCode
and 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
keyCode is standard JavaScript and of course not implemented in the same way by all browsers.
keyCode 是标准的 JavaScript,当然并非所有浏览器都以相同的方式实现。