使用onkeypress 1激活此功能

时间:2013-02-06 18:30:24

标签: 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 " 
}

  }

如何使用onkeydown激活此功能。我想用“1”键。我现在可以用任何键激活它。我希望它是1键

1 个答案:

答案 0 :(得分:0)

window.onkeydown = function (e) {
    if (e.which === 49 || e.keyCode === 49) {  // '1'.charCodeAt(0)
        addone();
    }

    return true; // allow normal keyboard output
};