How to detect pressing Enter on keyboard using jQuery?(如何使用 jQuery 检测键盘上的 Enter 键?)
问题描述
我想用jQuery检测用户是否按下了Enter.
I would like to detect whether the user has pressed Enter using jQuery.
这怎么可能?需要插件吗?
How is this possible? Does it require a plugin?
看来我需要使用 keypress()
一个>方法.
It looks like I need to use the keypress()
method.
我想知道是否有人知道该命令是否存在浏览器问题 - 比如有没有我应该知道的浏览器兼容性问题?
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?
推荐答案
jQuery 的全部意义在于您不必担心浏览器的差异.我很确定您可以安全地使用 enter 在所有浏览器中设置为 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!');
}
});
这篇关于如何使用 jQuery 检测键盘上的 Enter 键?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:如何使用 jQuery 检测键盘上的 Enter 键?


- 为什么悬停在委托事件处理程序中不起作用? 2022-01-01
- 在不使用循环的情况下查找数字数组中的一项 2022-01-01
- 使用 iframe URL 的 jQuery UI 对话框 2022-01-01
- 我不能使用 json 使用 react 向我的 web api 发出 Post 请求 2022-01-01
- 从原点悬停时触发 translateY() 2022-01-01
- 是否可以将标志传递给 Gulp 以使其以不同的方式 2022-01-01
- 如何调试 CSS/Javascript 悬停问题 2022-01-01
- 如何显示带有换行符的文本标签? 2022-01-01
- 为什么我的页面无法在 Github 上加载? 2022-01-01
- 如何向 ipc 渲染器发送添加回调 2022-01-01