Activate a Javascript function with a keypress [duplicate]

╄→гoц情女王★ 提交于 2019-12-25 18:26:15

问题


    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:


window.onkeypress = function(event) {
   if (event.keyCode == 41) {
      // do a function
   }
}

instead of onkeypress you can look at onkeydown / onkeyup

List of char codes: http://www.asciitable.com/



来源:https://stackoverflow.com/questions/14735197/activate-a-javascript-function-with-a-keypress

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!