<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
<style>
* {
margin: 0;
padding: 0;
}
h2 {
text-align: center;
}
.box {
width: 600px;
margin: 50px auto;
display: flex;
font-size: 25px;
line-height: 40px;
}
.qs {
width: 450px;
height: 40px;
color: red;
}
.btns {
text-align: center;
}
.btns button {
width: 120px;
height: 35px;
margin: 0 50px;
}
</style>
</head>
<body>
<h2>随机点名</h2>
<div class="box">
<span>名字是:</span>
<div class="qs">这里显示姓名</div>
</div>
<div class="btns">
<button class="start">开始</button>
<button class="end">结束</button>
</div>
<script>
const arr = ['小红', '小明', '小兰', '小紫', '小花']
const start=document.querySelector('.btns .start')
const qs=document.querySelector('.box .qs')
//定义全局变量
let n
let random
start.addEventListener('click',function(){
n=setInterval(function(){
random=Math.floor(Math.random()*arr.length)
qs.innerHTML=`${arr[random]}`
},50)
})
const end=document.querySelector('.btns .end')
end.addEventListener('click',function(){
clearInterval(n)
arr.splice(random,1)
if(arr.length===1){
start.disabled=true
end.disabled=true
}
})
</script>
</body>
</html>
随机点名(js实现)
这是一个使用HTML、CSS和JavaScript实现的简单随机点名程序。点击开始按钮,会从预设的名字列表中随机选取一个名字显示在页面上,每50毫秒更新一次。结束按钮用于清除定时器,并从列表中移除已点过的名字。当名单只剩下一个时,开始和结束按钮将被禁用。
摘要由CSDN通过智能技术生成