>>1.keyDown:当用户按下键盘上的任意键时触发,而且如果按住不放的话,会重复触发此事件;
>>2.keyPress:当用户按下键盘上的字符键时触发,而且如果按住不放的话,会重复触发此事件;
>>3.keyUp:当用户释放键盘上的键时触发;
>2.抽奖程序
var data = ['iPhone5','iPad','三星电脑','谢谢参与'], timer = null, flag = 0; window.onload = function(){ var play = document.getElementById('play'), stop = document.getElementById('stop'); //(鼠标)开始抽奖 play.onclick = palyFun; stop.onclick = stopFun; //(键盘Enter)开始抽奖 document.onkeyup = function(event){ event = event || window.event; if(event.keyCode == 13){ if(flag == 0){ palyFun(); flag = 1; }else{ stopFun(); flag = 0; } } } } function palyFun(){ var title = document.getElementById('title'), play = document.getElementById('play'); //清除之前的定时器,放置定时器重复; clearInterval(timer); //设置定时器; 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 paly = document.getElementById('play'); paly.style.background = '#036'; }
您可能感兴趣的文章: