關于鼠標和鍵盤混用的問題
鼠標和鍵盤單獨使用都沒問題。但是先用鼠標點擊開始,再敲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(); ????} }
2016-11-23
你這個我試過,是好的。能運行