用html js dom写实现一个随机点名功能页面
HTML代码:
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>随机点名</title>
<style>
#result {
font-size: 24px;
font-weight: bold;
margin-top: 20px;
}
</style>
</head>
<body>
<h1>随机点名</h1>
<button onclick="randomName()">点名</button>
<div id="result"></div>
<script src="script.js"></script>
</body>
</html>
JavaScript代码(script.js):
function randomName() {
var names = ["张三", "李四", "王五", "赵六", "钱七"];
var randomIndex = Math.floor(Math.random() * names.length);
var resultElement = document.getElementById("result");
resultElement.innerHTML = names[randomIndex];
}
这个页面中有一个按钮,当点击按钮时,会调用randomName函数来随机选择一个名字,并将结果显示在result元素中
原文地址: https://www.cveoy.top/t/topic/ixQa 著作权归作者所有。请勿转载和采集!