且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

如何使用jQuery检测按键盘输入?

更新时间:2022-04-28 08:52:35

jQuery的重点在于你不必担心浏览器差异。我很确定你可以安全地使用输入在所有浏览器中都是13。所以考虑到这一点,你可以这样做:

The whole point of jQuery is that you don't have to worry about browser differences. I am pretty sure you can safely go with enter being 13 in all browsers. So with that in mind, you can do this:

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