使用按键激活 Javascript 功能
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/14735197/
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
提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-26 22:27:11 来源:igfitidea点击:
Activate a Javascript function with a keypress
提问by muzzledog
function addone()
{
document.getElementById("subtract").disabled=false;
x += 1;
document.getElementById("result").innerHTML="Doshi has said OK " + x + " times."
if(x == 1 ){
document.getElementById("resetall").disabled=false;
document.getElementById("reset").disabled=false;
document.getElementById("result").innerHTML="Doshi has said OK " + x + " time."
}
if(x == 0){
document.getElementById("resetall").disabled=true;
document.getElementById("reset").disabled=true;
document.getElementById("result").innerHTML="Doshi has not said OK "
}
}
I want to start this function when I press the "1" key. How do I do this
当我按下“1”键时,我想启动这个功能。我该怎么做呢
回答by tobspr
window.onkeypress = function(event) {
if (event.keyCode == 41) {
// do a function
}
}
instead of onkeypress you can look at onkeydown / onkeyup
您可以查看 onkeydown / onkeyup 而不是 onkeypress
List of char codes: http://www.asciitable.com/
字符代码列表:http: //www.asciitable.com/