maki_S
2016-11-23 13:56
鼠标和键盘单独使用都没问题。但是先用鼠标点击开始,再敲enter键,这时候没有办法停止。不知道我的代码哪里有错。
var arr = ['R','R','R','R','R','R','R','R','R','R','SR','SR','SR','SR','SR','SSR']; var title = document.getElementById('title'), timer = null, flag = 0; window.onload = function(){ var start = document.getElementById('start'), stop = document.getElementById('stop'); //开始抽奖 start.onclick = startFunc; //停止抽奖 stop.onclick = stopFunc; //键盘事件 document.onkeyup = keyupFunc; } function startFunc(){ var title = document.getElementById('title'), start = document.getElementById('start'); clearInterval(timer); timer = setInterval(function(){ var random = Math.floor(Math.random()*arr.length); title.innerHTML = arr[random]; },50) start.disabled = true; start.style.backgroundColor = '#ccc'; flag = 1; } function stopFunc(){ clearInterval(timer); start.disabled = false; start.style.backgroundColor = '#1aa'; flag = 0; } function keyupFunc(event){ event = event || window.event; if(event.keyCode !== 13){ return null; }else{ flag == 0 ? startFunc() : stopFunc(); } }
你这个我试过,是好的。能运行
DOM事件探秘
99545 学习 · 1197 问题
相似问题
回答 3
回答 1
回答 2
回答 1
回答 2