这篇文章运用简单易懂的例子给大家介绍使用JavaScript实现一个刮刮乐效果,内容非常详细,感兴趣的小伙伴们可以参考借鉴,希望对大家能有所帮助。
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>Title</title> <style> .outer{ height: 90px; width: 300px; margin: 0 auto; margin-top: 5px; } .box{ height: 70px; width: 70px; background-color: darkgray; line-height: 70px; text-align: center; display: inline-block; margin-left: 5px; } </style> </head> <body> <div class="outer"> <div class="box"></div> <div class="box"></div> <div class="box"></div> </div> <div class="outer"> <div class="box"></div> <div class="box"></div> <div class="box"></div> </div> <div class="outer"> <div class="box"></div> <div class="box"></div> <div class="box"></div> </div> <script> var boxs = document.getElementsByClassName("box"); var item = ["一等奖","二等奖","三等奖","谢谢","谢谢","谢谢","谢谢","谢谢","谢谢",]; for (var i = 0;i<boxs.length;i++){ var box = boxs[i]; box.onclick = function (){ this.style.backgroundColor="red"; //this.innerText="一等奖";//随即从数组中获取内容,并且不能重复! var number = Math.floor(Math.random()*9); this.innerText = item[number]; this.style.color = "#fff"; this.style.verticalAlign = "top"; } } </script> </body> </html>
当前名称:使用JavaScript实现一个刮刮乐效果-创新互联
当前链接:https://www.cdcxhl.com/article6/dpgpig.html
成都网站建设公司_创新互联,为您提供面包屑导航、云服务器、用户体验、移动网站建设、微信公众号、建站公司
声明:本网站发布的内容(图片、视频和文字)以用户投稿、用户转载内容为主,如果涉及侵权请尽快告知,我们将会在第一时间删除。文章观点不代表本网站立场,如需处理请联系客服。电话:028-86922220;邮箱:631063699@qq.com。内容未经允许不得转载,或转载时需注明来源: 创新互联