關(guān)于鼠標(biāo)和鍵盤混用的問題
鼠標(biāo)和鍵盤單獨(dú)使用都沒問題。但是先用鼠標(biāo)點(diǎn)擊開始,再敲enter鍵,這時(shí)候沒有辦法停止。不知道我的代碼哪里有錯(cuò)。
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'); ????//開始抽獎(jiǎng) ????start.onclick?=?startFunc; ????//停止抽獎(jiǎng) ????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
你這個(gè)我試過,是好的。能運(yùn)行