函數(shù)放在onload外面也可以直接使用
<!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">結(jié)束</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>
2019-06-25
沒看懂你這是疑問句還是敘述句,onload事件是表示當頁面所有資源加載完畢之后才執(zhí)行事件內(nèi)的函數(shù)體代碼;所以外面也是可以放其他函數(shù)的