慕仔1126745
2019-08-10 20:40
<body>
<div class='title'>开始抽奖啦!</div>
<div class='btns' >
<span class='start'>开 始</span>
<span class='stop'>结 束</span>
</div>
<script>
var data=['红米','白米','小米','大米','黑米','恭喜抽中骚猪','谢谢惠顾'];
window.onload=function(){
var start=document.getElementsByClassName('start');
var stop=document.getElementsByClassName('stop');
var title=document.getElementsByClassName('title');
start.onclick=function(){
timer=setInterval(function(){
var random=Math.floor(Math.random()*data.length);
console.log(random);
// title.innerHTML=data[random];
},50);
}
}
</script>
</body>
<body><div class='title'>开始抽奖啦!</div> <div class='btns' ><button class='start'>开 始</button> <button class='stop'>结 束</button></div> <script> var data=['红米','白米','小米','大米','黑米','恭喜抽中骚猪','谢谢惠顾']; window.onload=function(){var start=document.getElementsByClassName('start'); var stop=document.getElementsByClassName('stop'); var title=document.getElementsByClassName('title'); start[0].onclick=function(){function timer(){var random=Math.floor(Math.random()*data.length); console.log(random); title[0].innerHTML=data[random];}setInterval(timer,50);}} </script> </body> 你注意看getElementsByClassName中间有s,它是一个对象集合。上述是改过之后的代码,你可以继续写完stop
DOM事件探秘
99545 学习 · 1197 问题
相似问题