1.代码区展示
<!DOCTYPE html>
<html lang="zh-CN">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>猜大小游戏</title>
<style>
body {
font-family: Arial, sans-serif;
text-align: center;
}
h1 {
color: #333;
}
button {
padding: 10px 20px;
font-size: 16px;
background-color: #007bff;
color: #fff;
border: none;
cursor: pointer;
}
button:hover {
background-color: #0056b3;
}
</style>
</head>
<body>
<h1>猜大小游戏</h1>
<p>我想一个1到10之间的数字,你猜是多少?</p>
<input type="number" id="userGuess" placeholder="请输入你的猜测">
<button οnclick="checkGuess()">猜一下</button>
<p id="result"></p>
<script>
function checkGuess() {
const randomNumber = Math.floor(Math.random() * 10) + 1;
const userGuess = parseInt(document.getElementById('userGuess').value);
if (userGuess === randomNumber) {
document.getElementById('result').innerText = '恭喜你,猜对了!';
} else {
document.getElementById('result').innerText = `很遗憾,正确数字是 ${randomNumber}`;
}
}
</script>
</body>
</html>
2.效果展示