<!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>
<body>
<button type="button" id="btn">小组抽奖</button>
<p id="showName"></p>
<div id="goodLuck" style="width: 100px; height: 40px"></div>
<script>
const arr = [
"何",
"冯",
"肖",
"唐",
"程",
"王",
"曾",
"张",
"付",
];
// 点击按钮 p 随机出现一个人名字
let a = arr.length;
//document.write(a)//9
// console.log(typeof(a));//number
const rand = parseInt(Math.random() * (a - 1 + 1) + 1); //rand范围1~9,
const showName = document.getElementById("showName");
const btn = document.getElementById("btn");
btn.onclick = function () {
showName.innerText = "你好啊~ " + arr[rand - 1]; //数组下标范围为0-8
};
</script>
</body>
</html>
js随机数——随机人名
最新推荐文章于 2023-08-04 11:46:29 发布