问答详情
源自:4-5 [DOM事件] 抽奖系统(下)

为什么我的键盘事件不响应?

var data=['三星','惠普','联想','谢谢惠顾','苹果','华硕','海尔电冰箱','谢谢惠顾'],
    timer=null,
    flag=0;

window.onload=myFun;
function myFun () {
    // body...
    var play=document.getElementById('play'),
        stop=document.getElementById('stop');
    play.onclick=playFun;
    stop.onclick=stopFun;
    //键盘事件
    document.onkeyup=function (event) {
        event=event||window.event;
        if(event.KeyCode==13){
            if (flag==0) {
                playFun();
                flag=1;
            }
            else{
                stopFun();
                flag=0;
            }
        }
        
    }
}
function playFun () {
    var title=document.getElementById('title'),
        play=document.getElementById('play');
    timer=setInterval(function(){
        var random=Math.floor(Math.random()*data.length);
        title.innerHTML=data[random];
    },50);
    play.style.background="#999";
}
function stopFun () {
    clearInterval(timer);
    var play=document.getElementById('play');
    play.style.background="#036";
}

提问者:qq_请叫我曼哥好么_0 2016-11-17 16:13

个回答

  • qq_请叫我曼哥好么_0
    2016-11-17 16:53:07

    keyCode中的k要小写