鼠标点击事件,键盘事件。 1. [代码] [JavaScript]代码 !doctype htmlhtmlhead meta charset="utf-8"title抽奖/titlestyle .title{text-align:center;line-height:5; width: 220px;} .start,.stop{width:100px;height:30px;line-height:30p
1. [代码][JavaScript]代码
<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>抽奖</title>
<style>
.title{text-align:center;line-height:5; width: 220px;}
.start,.stop{width:100px;height:30px;line-height:30px;font-size:14px;background-color:red;color:white;float:left;margin-left:10px;text-align:center; cursor:pointer;}
</style>
</head>
<body>
<div class="title" id="title">开始中奖</div>
<div class="start" id="start">开始</div>
<div class="stop" id="stop">停止</div>
<script>
var data=['Phone5','Ipad','三星笔记本','佳能相机','惠普打印机','谢谢参与','50元充值卡','1000元超市购物券'],
timer=null,
flag=0;
window.onload=function(){
var start=document.getElementById('start'),
stop=document.getElementById('stop');
start.onclick=startFun;
stop.onclick=stopFun;
document.onkeyup=function(event){
event=event||window.event;
if(event.keyCode==13){
if(flag==0){
startFun();
flag=1;
}else{
stopFun();
flag=0;
}
}
}
}
function startFun(){
var title=document.getElementById('title'),
start=document.getElementById('start');
clearInterval(timer); //关闭定时器
timer=setInterval(function(){ //开启定时器
var random=Math.floor(Math.random()*data.length); //向上取整(随机*数组个数)
title.innerHTML=data[random]; //获取数组的值
},50)
start.style.backgroundColor='#999';
}
function stopFun(){
var start=document.getElementById('start');
clearInterval(timer);
start.style.backgroundColor='red';
}
</script>
</body>
</html>
