JavaScript 运行函数 keydown
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/6254651/
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 run function keydown
提问by Jeremy Karlsson
I cant find an answer to this that I understand. I want with JavaScript (not jQuery) to make it so if the with the id boxFilm is visible and I click on "SPACE" (keycode 32), the function togglep(); runs.
我找不到我理解的答案。我想使用 JavaScript(不是 jQuery)来实现,如果 id 为 boxFilm 的可见并且我单击“SPACE”(键码 32),则函数 togglep(); 运行。
How do I do this? I've tried many things but haven't succeeded :(
我该怎么做呢?我尝试了很多东西,但没有成功:(
回答by wong2
function togglep(){
alert("Hi");
}
document.body.onkeydown = function(event){
event = event || window.event;
var keycode = event.charCode || event.keyCode;
if(keycode === 32){
togglep();
}
}
try it here: http://jsfiddle.net/GuvRP/1/
在这里试试:http: //jsfiddle.net/GuvRP/1/