编写html点名代码从ABCD四个学生中抽出三个点击开始按钮就开始不断抽点击完成按钮停止抽并显示结果。
<!DOCTYPE html>
<html>
<head>
<title>HTML点名器</title>
<script>
var studentList = ["A", "B", "C", "D"]; // 学生名单
var chosenList = []; // 已选学生名单
var timer; // 定时器
<pre><code> function start() {
timer = setInterval(function() {
var randomIndex = Math.floor(Math.random() * studentList.length); // 生成随机数
var chosenStudent = studentList[randomIndex]; // 选中学生
chosenList.push(chosenStudent); // 将已选学生添加到列表中
document.getElementById("result").innerHTML = chosenList.join(", "); // 显示已选学生
}, 100); // 每100毫秒抽一次
}
function stop() {
clearInterval(timer); // 停止定时器
}
</script>
</code></pre>
</head>
<body>
<h1>HTML点名器</h1>
<p>从A,B,C,D四个学生中抽出三个</p>
<button onclick="start()">开始</button>
<button onclick="stop()">完成</button>
<p>已选学生名单:</p>
<p id="result"></p>
</body>
</html
原文地址: https://www.cveoy.top/t/topic/e9Aq 著作权归作者所有。请勿转载和采集!