在html运行的 先在html创建第几次和得分的文本框, JS 将function handIn() { var total = 0; //遍历答题卡和答案 for (var i = 0; i < answers.length; i++) { if (answers[i].toLowerCase() === keys[i]) { total = total + Number(p[i].score); } }} alert('你的得分是:' + total);的total储存到localStorage的一个数组,并且html点击一个查询按钮将从localStorage中获取数组并将每个total得分添加到html的得分的文本框中,把每次total得分的序号显示第几次文本框,html点击一个查询将的html和js代码
时间: 2024-03-21 10:38:38 浏览: 50
圣诞树代码,直接运行html就可以了,送女朋友的惊喜
以下是HTML和JS代码:
HTML代码:
```html
<!DOCTYPE html>
<html>
<head>
<title>Quiz</title>
</head>
<body>
<h1>Quiz</h1>
<p>Answer the following questions:</p>
<div>
<p>1. What is the capital of France?</p>
<input type="text" id="answer1">
</div>
<div>
<p>2. What is the tallest mountain in the world?</p>
<input type="text" id="answer2">
</div>
<div>
<p>3. Who is the author of the Harry Potter series?</p>
<input type="text" id="answer3">
</div>
<button onclick="handIn()">Submit</button>
<div>
<p>Your score is:</p>
<input type="text" id="score1">
</div>
<div>
<p>Your score is:</p>
<input type="text" id="score2">
</div>
<div>
<p>Your score is:</p>
<input type="text" id="score3">
</div>
<button onclick="displayScores()">Display Scores</button>
<script src="quiz.js"></script>
</body>
</html>
```
JS代码:
```js
// Define the correct answers and scores
var answers = ['paris', 'mount everest', 'j.k. rowling'];
var scores = [10, 10, 10];
// Define the function that handles the submission
function handIn() {
// Get the answers from the input fields
var answer1 = document.getElementById('answer1').value.toLowerCase();
var answer2 = document.getElementById('answer2').value.toLowerCase();
var answer3 = document.getElementById('answer3').value.toLowerCase();
// Define the array to store the scores
var scoreArray = [];
// Check the answers and calculate the total score
var total = 0;
for (var i = 0; i < answers.length; i++) {
if (answers[i] === answer1 || answers[i] === answer2 || answers[i] === answer3) {
total += scores[i];
scoreArray.push(scores[i]);
} else {
scoreArray.push(0);
}
}
// Store the score array in localStorage
localStorage.setItem('scores', JSON.stringify(scoreArray));
// Display the total score
alert('Your score is: ' + total);
}
// Define the function that displays the scores
function displayScores() {
// Get the score array from localStorage
var scoreArray = JSON.parse(localStorage.getItem('scores'));
// Display the scores in the input fields
document.getElementById('score1').value = scoreArray[0];
document.getElementById('score2').value = scoreArray[1];
document.getElementById('score3').value = scoreArray[2];
}
```
注意:这段代码并不完整,仅供参考。在实际应用中,需要添加更多的验证和错误处理,以确保代码的健壮性和安全性。
阅读全文