我认为我的逻辑遇到了问题。应该发生的是,用户将输入所提出问题的答案。提交问题后,当前文本(第一个问题)就会消失,数组中的下一个问题会出现在第一个问题的位置。
目前,问题仍在不断堆积,而没有删除前一个问题。每次我尝试添加更多代码来删除该元素时,都会出现错误。
请帮忙!我正在慢慢添加此代码功能,但现在我陷入了添加/删除文本的困境。
$(function() {
$("#submit").on("click", askQuestion)
var questions = [{
question: "Is the price higher or lower than $40.00?",
answer: "higher"
},
{
question: "Is the price higher or lower than $100.00?",
answer: "higher"
},
{
question: "Is the price higher or lower than $50.00?",
answer: "lower"
}
];
function askQuestion() {
if (answer && document.querySelector("#user-answer").value == answer) {
document.querySelector("#correct").style.display = "block";
document.querySelector("#sorry").style.display = "none";
answer = randomQuestion()
} else {
document.querySelector("#correct").style.display = "none";
document.querySelector("#sorry").style.display = "block";
}
}
function randomQuestion() {
var question = questions[Math.floor(Math.random() * questions.length)];
document.querySelector("#user-answer").value = "";
var element = document.createElement("div");
element.innerHTML = question.question;
$("#question").append(element.firstChild);
return question.answer;
}
var answer = randomQuestion();
});
<html>
<head>
<meta charset="UTF-8">
<title>Game time</title>
</head>
<body>
<h1>Question and Answer</h1>
<div>
<h2 id="question"></h2>
</div>
<label for="text">Answer:</label>
<input id="user-answer" type="text" value="">
<button id="submit" type="submit">Submit</button>
<p id="sorry" style="display: none">Sorry...</p>
<p id="correct" style="display: none">You got it!</p>
<script src="https://code.jquery.com/jquery-3.3.1.min.js" integrity="sha256-FgpCb/KJQlLNfOu91ta32o/NMZxltwRo8QtmkMRdAu8=" crossorigin="anonymous"></script>
<script src="main.js"></script>
</body>
</html>
最佳答案
当您应该更改问题时,您只需使用append
,而不是附加到它。
您可以使用:
$("#question").html(element.firstChild);
而不是$("#question").append(element.firstChild);
$(function() {
$("#submit").on("click", askQuestion)
var questions = [{
question: "Is the price higher or lower than $40.00?",
answer: "higher"
},
{
question: "Is the price higher or lower than $100.00?",
answer: "higher"
},
{
question: "Is the price higher or lower than $50.00?",
answer: "lower"
}
];
function askQuestion() {
if (answer && document.querySelector("#user-answer").value == answer) {
document.querySelector("#correct").style.display = "block";
document.querySelector("#sorry").style.display = "none";
answer = randomQuestion()
} else {
document.querySelector("#correct").style.display = "none";
document.querySelector("#sorry").style.display = "block";
}
}
function randomQuestion() {
var question = questions[Math.floor(Math.random() * questions.length)];
document.querySelector("#user-answer").value = "";
var element = document.createElement("div");
element.innerHTML = question.question;
$("#question").html(element.firstChild);
return question.answer;
}
var answer = randomQuestion();
});
<html>
<head>
<meta charset="UTF-8">
<title>Game time</title>
</head>
<body>
<h1>Question and Answer</h1>
<div>
<h2 id="question"></h2>
</div>
<label for="text">Answer:</label>
<input id="user-answer" type="text" value="">
<button id="submit" type="submit">Submit</button>
<p id="sorry" style="display: none">Sorry...</p>
<p id="correct" style="display: none">You got it!</p>
<script src="https://code.jquery.com/jquery-3.3.1.min.js" integrity="sha256-FgpCb/KJQlLNfOu91ta32o/NMZxltwRo8QtmkMRdAu8=" crossorigin="anonymous"></script>
<script src="main.js"></script>
</body>
</html>
关于javascript - 使用 jQuery/JavaScript 做出答案后删除文本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51142832/