<DOCTYPE?html>
<html>
<head>
<meta?charset="UTF-8">
<title>content</title>
<style?type="text/css">
html,body,div,button{margin:?0;padding:?0;}
.title{margin:?0?auto;width:?300px;height:?100px;margin-top:?50px;text-align:?center;color:?#f00;font-weight:?bold;font-size:30px;?}
.btns{width:?300px;height:?30px;margin:?0?auto;}
button{display:?block;float:?left;
width:?100px;height:?25px;line-height:?25px;background-color:?orange;border:?1px?solid?#eee;border-radius:?5px;outline:?none;cursor:?pointer;color:#fff;font-family:?"微軟雅黑"
}
#stop{float:?right;}
</style>
</head>
<body>
<div?class="title"?id="title">開始抽獎</div>
<div?class="btns">
<button?id="start">開始</button><button?id="stop">停止</button>
</div>
<script?type="text/javascript">
//?綁定事件
window.onload=function(){
var?start=document.getElementById("start"),
stop=document.getElementById("stop");
dontknow.addEvent(start,"click",action);
dontknow.addEvent(stop,"click",mystop);
}
var?data=["iphone5","ipad","三星筆記本","佳能相機","50元充值券","謝謝參與"],
timer=null,
dontknow={
addEvent:function(ele,type,handle){
ele.addEventListener?ele.addEventListener(type,handle):ele.attachEvent("on"+type,handle);
},
removeEvent:function(ele,type,handle){
ele.removeEventListener?ele.removeEventListener(type,handle):ele.detachEvent("on"+type,handle);
}
};
var?flag=true;?//判斷抽獎是否開始進行,若開始則flag=false
//?抽獎開始函數(shù)
function?action(){
var?otitle=document.getElementById("title");
var?start=document.getElementById("start");
if(timer==null){
timer=setInterval(change,50);
start.style.backgroundColor="#ccc";
flag=!flag;}
};
//?獎品改變函數(shù)
function?change(){
var?otitle=document.getElementById("title");
var?num=Math.floor(Math.random()*data.length);
otitle.innerHTML=data[num];
};
//?抽獎停止函數(shù)
function?mystop(){
var?start=document.getElementById("start");
start.style.backgroundColor="orange";
clearInterval(timer);
timer=null;
flag=!flag;
}
//?鍵盤事件?keyDown??keyPress?keyUp???
?document.onkeyup=function(event){
? event=event||window.event;
? if(event.keyCode==13){
? if(flag){
? action();
? }
? else?if(!flag){
? mystop();
? }
? }
?};
</script>
</body>
</html>
2016-05-26
不知道樓主有沒有找到原因,同問
2016-03-08
?
?dontknow={
????????????addEvent:function(ele,type,handle){
????????????????ele.addEventListener?ele.addEventListener(type,handle):ele.attachEvent("on"+type,handle);
????????????},
????????????removeEvent:function(ele,type,handle){
????????????????ele.removeEventListener?ele.removeEventListener(type,handle):ele.detachEvent("on"+type,handle);
????????????}
????????};
將此換成
start.onclick=action;
stop.onclick=mystop;
之后你所說的問題就解決了但是會出現(xiàn):
某次用鼠標操作之后再按回車鍵就不起作用了