html做一个上课随机点名系统
时间: 2023-11-06 08:08:46 浏览: 82
<!DOCTYPE html>
<html>
<head>
<title>上课随机点名系统</title>
<meta charset="utf-8">
<style type="text/css">
body {
background-color: #f5f5f5;
font-family: Arial, sans-serif;
margin: 0;
padding: 0;
}
h1 {
text-align: center;
margin-top: 50px;
}
.container {
display: flex;
flex-wrap: wrap;
justify-content: center;
margin-top: 50px;
}
.student {
width: 100px;
height: 100px;
background-color: #fff;
border-radius: 50%;
display: flex;
flex-direction: column;
align-items: center;
justify-content: center;
margin: 10px;
box-shadow: 0 5px 10px rgba(0, 0, 0, 0.1);
cursor: pointer;
}
.student:hover {
box-shadow: 0 5px 15px rgba(0, 0, 0, 0.2);
}
.student span {
font-size: 24px;
color: #333;
margin-top: 10px;
text-align: center;
word-wrap: break-word;
white-space: normal;
max-width: 80%;
}
.selected {
background-color: #ffcc66;
}
.btn {
display: block;
margin: 50px auto 0;
padding: 10px 20px;
background-color: #333;
color: #fff;
font-size: 18px;
border: none;
border-radius: 5px;
cursor: pointer;
}
.btn:hover {
background-color: #666;
}
</style>
</head>
<body>
<h1>上课随机点名系统</h1>
<div class="container"></div>
<button class="btn">开始点名</button>
<script type="text/javascript">
var students = [
{name: "张三", id: "001"},
{name: "李四", id: "002"},
{name: "王五", id: "003"},
{name: "赵六", id: "004"},
{name: "钱七", id: "005"},
{name: "孙八", id: "006"},
{name: "周九", id: "007"},
{name: "吴十", id: "008"},
{name: "郑十一", id: "009"},
{name: "冯十二", id: "010"}
];
var container = document.querySelector(".container");
var btn = document.querySelector(".btn");
// 初始化学生列表
for (var i = 0; i < students.length; i++) {
var student = document.createElement("div");
student.classList.add("student");
student.setAttribute("data-id", students[i].id);
var initials = students[i].name.split("").filter(function(char) {
return char >= "A" && char <= "Z";
}).join("");
student.innerHTML = "<span>" + initials + "</span>";
container.appendChild(student);
}
// 点击学生时选中
container.addEventListener("click", function(event) {
var target = event.target;
if (target.classList.contains("student")) {
target.classList.toggle("selected");
}
});
// 点击开始点名按钮时随机选中一个学生
btn.addEventListener("click", function() {
var selectedStudents = document.querySelectorAll(".selected");
if (selectedStudents.length > 0) {
for (var i = 0; i < selectedStudents.length; i++) {
selectedStudents[i].classList.remove("selected");
}
}
var randomIndex = Math.floor(Math.random() * students.length);
var selectedStudent = document.querySelector(".student[data-id='" + students[randomIndex].id + "']");
selectedStudent.classList.add("selected");
});
</script>
</body>
</html>
阅读全文