以下是使用JavaScript实现随机点名的示例代码:

<!DOCTYPE html>
<html>
<head>
	<title>Random Name Picker</title>
	<meta charset="UTF-8">
	<style>
		body {
			font-family: Arial, sans-serif;
			margin: 0;
			padding: 0;
		}
		h1 {
			text-align: center;
		}
		button {
			font-size: 20px;
			padding: 10px 20px;
			border: none;
			background-color: #4CAF50;
			color: #fff;
			cursor: pointer;
			border-radius: 5px;
			margin: 20px;
		}
		#result {
			font-size: 40px;
			text-align: center;
			margin: 50px 0;
		}
	</style>
</head>
<body>
	<h1>Random Name Picker</h1>
	<div id="result"></div>
	<div style="text-align: center;">
		<button onclick="pickRandomName()">Pick a name!</button>
	</div>
	<script>
		const names = ['Alice', 'Bob', 'Charlie', 'David', 'Emma', 'Frank', 'Grace', 'Henry', 'Isabella', 'Jack', 'Katherine', 'Liam', 'Megan', 'Nathan', 'Olivia', 'Peter', 'Quinn', 'Rachel', 'Sophia', 'Thomas', 'Ursula', 'Victor', 'Wendy', 'Xander', 'Yvonne', 'Zachary'];

		function pickRandomName() {
			const result = document.getElementById("result");
			const randomIndex = Math.floor(Math.random() * names.length);
			result.textContent = names[randomIndex];
		}
	</script>
</body>
</html>

在此示例中,我们定义了一个名为'names'的数组,其中包含要从中随机选择的名称。当单击按钮时,我们将使用Math.random()函数生成一个介于0(包括)和1(不包括)之间的随机数字,并使用Math.floor()函数将其转换为介于0和names.length(不包括)之间的整数。然后,我们将使用该随机索引从names数组中选择一个名称,并将其设置为result元素的文本内容,以便用户可以看到随机选择的名称。

请注意,在此示例中,我们使用了一个简单的HTML页面和内联CSS样式来创建页面布局和样式。您可以根据自己的需要自定义它们。

JavaScript 随机点名工具 - 在线生成随机姓名

原文地址: https://www.cveoy.top/t/topic/mq18 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录