<!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>
div {
width: 400px;
height: 200px;
background-color: #ccc;
border-radius: 2%;
line-height: 200px;
}
button {
margin-top: 20px;
width: 400px;
height: 100px;
border: 1px solid #333;
border-radius: 2%;
line-height: 100px;
}
div,
button {
margin-left: 500px;
font-size: 40px;
text-align: center;
}
p {
width: 100px;
border: 2px solid #333;
text-align: center;
font-size: 30px;
background-color: #aaa;
border-radius: 5px;
margin-top: -200px;
}
</style>
</head>
<body>
<div id="name"></div>
<button onclick="start(this)">开始</button>
<p id='getName'></p>
</body>
<script>
var arr = ['张三', '李四', '王五', '张三丰', '李宁']
var timer = null
var index = 0
function start(ele) {
if (arr.length <= 0) {
document.getElementById('name').innerHTML = '结束'
ele.innerHTML = '重新开始'
arr = ['张三', '李四', '王五', '张三丰', '李宁']
return
}
if (ele.innerHTML == '重新开始') {
ele.innerHTML = '开始'
document.getElementById('name').innerHTML = ''
document.getElementById('getName').innerHTML = ''
return
}
if (ele.innerHTML == '开始') {
ele.innerHTML = '停止'
timer = setInterval(randName, 35)
} else {
ele.innerHTML = '开始'
clearInterval(timer)
//把抽取的名字放在p标签中
document.getElementById('getName').innerHTML += arr[index] + '<br>'
//从数组中删除这个名字
arr.splice(index, 1)
}
}
function randName() {
var index = Math.floor(Math.random() * arr.length)
//获取随机名字
document.getElementById('name').innerHTML = arr[index]
}
</script>
</html>
随机点名3.0
最新推荐文章于 2023-01-11 22:57:43 发布