如何使用js 自行设计界面,完成小学生100以内数的加法训练,要求随机生成50以内的 加数和被加数,让用户从文本框输入和,并验证用户输入和的正确性,如果正确,在用户输入文本框下方显示绿色 “恭喜你,答对了!”文字,否则显示红色“错了,再想一想?”的文字。
时间: 2023-06-07 08:11:49 浏览: 117
您可以使用HTML和JavaScript来实现上述需求。首先,您需要创建一个包含两个数字相加的问题的随机数生成器。然后,您需要创建一个文本框让用户输入答案,并验证它是否与预期答案相匹配。最后,当用户输入的答案正确时,在文本框下面显示绿色“恭喜你,答对了!”的文字,否则显示红色“错了,再想一想?”的文字。
以下是一个基本的实现示例:
```html
<!DOCTYPE html>
<html>
<head>
<title>小学生100以内数的加法训练</title>
<script>
function generateQuestion() {
// 生成两个数字相加的问题
var num1 = Math.floor(Math.random() * 50) + 1;
var num2 = Math.floor(Math.random() * 50) + 1;
var result = num1 + num2;
// 显示问题并存储答案
document.getElementById("question").innerHTML = num1 + " + " + num2 + " = ";
document.getElementById("answer").value = result;
document.getElementById("message").innerHTML = "";
document.getElementById("answer").style.backgroundColor = "";
}
function checkAnswer() {
// 获取用户输入的答案
var userAnswer = document.getElementById("answer").value;
// 获取正确答案并比较用户答案
var correctAnswer = document.getElementById("answer").getAttribute("value");
if (userAnswer == correctAnswer) {
// 用户答案正确
document.getElementById("message").innerHTML = "恭喜你,答对了!";
document.getElementById("answer").style.backgroundColor = "#C7E0BE";
} else {
// 用户答案错误
document.getElementById("message").innerHTML = "错了,再想一想?";
document.getElementById("answer").style.backgroundColor = "#FBBDBF";
}
}
</script>
</head>
<body onload="generateQuestion()">
<h1>小学生100以内数的加法训练</h1>
<div>
<p id="question"></p>
<input type="text" id="answer">
<button onclick="checkAnswer()">确认答案</button>
<p id="message"></p>
</div>
</body>
</html>
```
在上面的代码中,generateQuestion()函数用于生成随机问题和正确答案,并将它们显示在页面上。checkAnswer()函数在用户输入答案后被调用,将正确答案与用户输入的答案进行比较,并根据其是否匹配来显示相应的信息和文本框背景。页面的onload属性用于在页面加载后立即调用generateQuestion()函数,以便显示首个随机问题。
相关推荐
![xls](https://img-home.csdnimg.cn/images/20210720083658.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)