按空格触发的 Javascript 事件
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/6199038/
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
Javascript event triggered by pressing space
提问by Henrik Skogmo
I am trying to get an event to trigger when I am on a page and press space, but I can't figure it out. Currently I am trying to use jQuery to accomplish a satisfying result.
当我在页面上并按下空格时,我试图触发一个事件,但我无法弄清楚。目前我正在尝试使用 jQuery 来完成令人满意的结果。
I have tried using keydown, keyup and keypress. But it seems that you can only use it if you are actually inputting something to a form or field.
我曾尝试使用 keydown、keyup 和 keypress。但似乎只有在您实际向表单或字段输入内容时才能使用它。
What I want is to trigger an alert when space is pressed.
我想要的是在按下空格时触发警报。
Thank you in advance!
先感谢您!
回答by Félix Saparelli
These events bubble up, so if you're trying to trigger the event wherever your focus is (ie. not in an input), just bind a handler on window
:
这些事件会冒泡,因此如果您尝试在焦点所在的任何地方(即不在输入中)触发事件,只需在 上绑定一个处理程序window
:
$(window).keypress(function (e) {
if (e.key === ' ' || e.key === 'Spacebar') {
// ' ' is standard, 'Spacebar' was used by IE9 and Firefox < 37
e.preventDefault()
console.log('Space pressed')
}
})
Also see the list of all .key
values.
另请参阅所有.key
值的列表。
回答by thecodeparadox
Try this:
尝试这个:
$('input:text').keypress(function(e) {
if (e.keyCode == 0 || e.keyCode == 32) // `0` works in mozilla and `32` in other browsers
console.log('space pressed');
});
回答by Karl-Bj?rnar ?ie
Try to bind your key event listener to the jQuery $(document) object;
尝试将您的按键事件侦听器绑定到 jQuery $(document) 对象;
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01//EN">
<html>
<head>
<script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/1.6.1/jquery.min.js"></script>
<script type="text/javascript">
$(document).ready(function() {
$(document).keydown(function(e) {
if (e.keyCode == '32') {
alert('space');
}
});
});
</script>
</head>
<body>
</body>
</html>
回答by DJ18
This code can be used:
可以使用此代码:
$(window).keypress(function(e) {
if (e.keyCode == 0 || e.keyCode == 32) {
console.log('Space pressed');
}
});
Explaination:
The $(window).keypress(function(e)
waits for the user to press any key and stores the data of the key pressed in the argument 'e'.
Then if (e.keyCode == 0 || e.keyCode == 32)
checks if the code of the key pressed is equal to the code of spacebar, that is 0 or 32. If this returns false then any other key is pressed and the code ends.
Some commonly used keycodes:
释:
在$(window).keypress(function(e)
等待用户按下任何键和存储密钥的数据在参数“e”的按压。
然后if (e.keyCode == 0 || e.keyCode == 32)
检查按下的键的代码是否等于空格键的代码,即 0 或 32。如果返回 false,则按下任何其他键,代码结束。
一些常用的键码:
- backspace:8
- tab:9
- enter:13
- shift:16
- ctrl:17
- alt:18
- caps lock:20
- escape:27
- (space):32
- 0-9:48-57
- a-z:65-90
- numpad0-numpad9:96-105
- 退格:8
- 标签:9
- 输入:13
- 班次:16
- 控制:17
- 替代:18
- 大写锁定:20
- 逃脱:27
- (空格):32
- 0-9:48-57
- AZ:65-90
- 小键盘0-小键盘9:96-105