Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
281 views
in Technique[技术] by (71.8m points)

javascript - 如何使用jQuery检测按键盘输入?(How to detect pressing Enter on keyboard using jQuery?)

I would like to detect whether the user has pressed Enter using jQuery.

(我想检测用户是否使用jQuery按Enter键 。)

How is this possible?

(这怎么可能?)

Does it require a plugin?

(它需要插件吗?)

EDIT: It looks like I need to use the keypress() method.

(编辑:看起来我需要使用keypress()方法。)

I wanted to know if anyone knows if there are browser issues with that command - like are there any browser compatibility issues I should know about?

(我想知道是否有人知道该命令是否存在浏览器问题 - 比如我是否应该了解任何浏览器兼容性问题?)

  ask by chris translate from so

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

The whole point of jQuery is that you don't have to worry about browser differences.

(jQuery的重点在于您不必担心浏览器差异。)

I am pretty sure you can safely go with enter being 13 in all browsers.

(我很确定你可以放心地在所有浏览器中输入 13。)

So with that in mind, you can do this:

(所以考虑到这一点,你可以这样做:)

$(document).on('keypress',function(e) {
    if(e.which == 13) {
        alert('You pressed enter!');
    }
});

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...