jQuery 捕获在页面上任意位置按下的 Enter 键
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/7005162/
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
Capture an Enter Key Pressed anywhere on the page
提问by Adam
I need to capture an Enter Key press at any time anywhere on a logon page. It will initiate a logon attempt.
我需要随时随地在登录页面上的任何位置捕获 Enter 键按下。它将启动登录尝试。
Using jQuery, how would I accomplish this? And would I link it to the body tag?
使用 jQuery,我将如何做到这一点?我会把它链接到 body 标签吗?
回答by sje397
$(document).keypress(function(e) {
if(e.which == 13) {
// enter pressed
}
});
回答by canoe
The keydown event is fired when a key is pressed down.
The keypress event is fired when a key is pressed down and that key normally produces a character value.
当按下某个键时会触发 keydown 事件。
当一个键被按下并且该键通常会产生一个字符值时会触发 keypress 事件。
$(document).keydown( function(event) {
if (event.which === 13) {
// login code here
}
});