<!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>
</head>
<style>
div{
height: 50px;
width: 100px;
border: black solid 1px;
text-align: center;
}
</style>
<body>
<div> 开始抽奖 </div>
<button> 开始点名 </button>
<script>
// 2.拿到绑定事件
let btn = document.querySelector('button')
let box = document.querySelector('div')
let arr = ['小明','小花','小军','小李','小万','小卷毛']
// 1.随机数 公式(max -min +1) + min
function getRandow(m,n){
return Math.floor(Math.random() * (m-n+1) ) + n;
}
// console.log(arr[random])
btn.addEventListener('click',function(){
//随机的名字
let random = getRandow(0,arr.length-1)
box.innerHTML = arr[random]
//删除数组里面的元素 solice (从哪里删除,删除几个)
arr.splice(random,1)
//如果数组 没有了长度 为0 ,就要禁用按钮
if(arr.length === 0){
btn.disabled = true
btn.innerHTML = "已经抽完了"
}
})
</script>
</body>
</html>
做一个抽奖的小功能
最新推荐文章于 2024-02-28 17:31:48 发布