使用按键激活Javascript函数

时间:2013-02-06 17:30:06

标签: javascript onkeydown

    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 "
}
    }

我想在按“1”键时启动此功能。我该怎么做

1 个答案:

答案 0 :(得分:2)

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

而不是onkeypress你可以看看onkeydown / onkeyup

字符代码列表:http://www.asciitable.com/