JavaScript +html随机点名系统

<!DOCTYPE html>
<html>
	<head>
		<meta charset="utf-8">
		<title></title>
		<style type="text/css">
		*{
		            margin: 0px;
		            padding: 0px;
		        }
		        .div1{
		            width: 800px;
		            height: 300px;
					border-radius:25px,50px;
		        }
		        .yangshi{
		            height: 100px;
		            width: 100px;
		            border-radius:15px;
		            display: inline-block;
		            margin-left: 20px;
		            text-align: center;
		            line-height: 100px;
		            background-color: pink;
		            margin-bottom: 30px;
		          
		        }
		        #button{
		            width: 200px;
		            height: 50px;
		            background-color: pink;
					margin-left: 200px;
					top: auto;
					border-radius:25px;
		        }
			
		</style>
	</head>
	<body>
		<div id="div1" class="div1"></div>
		    <button value="开始" id="button" onclick="">开始</button>
		    <script>
		        /* 一个名字一个div */
		        
		        var arrs =["1","2","3","4","5","6","7","8","9","10","11"]; 
		        /*用后台模拟网页数据   以上用数组分别代表不同的同学*/
		        var div1=document.getElementById("div1");/*   获得网页div的id*/
		        for(var i=0;i<arrs.length;i++){/* 循环数组,创建数组相对应的div */
		            var div=document.createElement("div");
		            div.innerHTML=arrs[i];
		            div.className="yangshi";
		            div1.appendChild(div);  
		        }
		        var but=document.getElementById("button");
		        var s= but.getAttribute("value");// 获取but的value值
		        console.log(s);/* 验证是否获取到*/
		        // function a(){
		        //     console.log(this)
		           
		        // }
		        /* 给button添加点击事件*/
		        but.onclick=function(){
		  			/* 如果当 but的value值为 “开始” 然后执行*/
		             if(s==="开始"){
		                 x= setInterval(function(){
		                	// 清空所有样式
		                    for(var j=0;j<arrs.length;j++){
		                    div1.children[j].style.background="";
		                    }
		                    /*开始随机数给随机到的div背景颜色*/
		                    var random=parseInt(Math.random()*(arrs.length-1+1-0)+0)
		                    div1.children[random].style.background="antiquewhite";    
		                }, 100);
		                s="停止"
		                but.innerHTML="停止"
		            }else{
		                //console.log("x",x);
		                clearInterval(x);  //不满足条件是清除计时器
		                //重新给button 给值
		                s="开始"
		                but.innerHTML="开始"
		            }       
		        }        
		    </script>
		
	</body>
</html>

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值