<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Document</title>
</head>
<style>
*{margin:0;padding:0}
#title{width: 400px;height: 70px;margin:0 auto;padding-top: 30px;text-align: center;font-size: 24px;color: #f00;font-weight: bold;}
.btn{width: 190px;height: 30px;margin:0 auto;}
.btn span{display:block;float: left;width: 80px;height: 25px;line-height: 25px;background: #036;border: 1px soli #eee;border-radius: 8px;margin-right: 10px;color: #fff;text-align: center;font-size: 14px;cursor: pointer;}
</style>
<body>
<div id="title">开始抽奖</div>
<div class="btn">
<span id="play">开始</span>
<span id="stop">结束</span>
</div>
<script>
var data=["一","二","三","四","五","六","七","八"],
timer=null,
a=0;
window.onload=function(){
var title=document.getElementById('title'),
play=document.getElementById('play'),
stop=document.getElementById('stop');
play.onclick=playFun;
stop.onclick=stopFun;
}
function playFun(){
clearInterval(timer);
timer=setInterval(function(){
var random=Math.floor(Math.random()*data.length);
title.innerHTML=data[random];
},50);
play.style.background='#999';//这里直接使用play
}
function stopFun(){
clearInterval(timer);
play.style.background='#036';//这里直接使用play
}
document.onkeyup=function(e){
e=e||window.event;
if(e.keyCode==13){
a=!a;
if(a){
playFun();
}else{
stopFun();
}
}
}
</script>
</body>
</html>
没看懂你这是疑问句还是叙述句,onload事件是表示当页面所有资源加载完毕之后才执行事件内的函数体代码;所以外面也是可以放其他函数的