【发布时间】:2022-02-12 17:53:24
【问题描述】:
我需要使变量“answer”和“submit”不可见,我尝试在 javascript 文件的第 21-22 行执行此操作,但它不起作用 我怎样才能让它们不可见?
javascript 和 html 代码:
let question = document.getElementById("question");
let answer = document.getElementById("answer");
let submit = document.getElementById("submit");
//create an array of questions
let questions = ["What type of file do you need to open javascript file ?", "question 2", "question 3", "question 4", "question 5", "question 6", "question 7", "question 8", "question 9", "question 10"];
//create an array of answers
let answers = ["js", "answer 2", "answer 3", "answer 4", "answer 5", "answer 6", "answer 7", "answer 8", "answer 9", "answer 10"];
// set the question varible to the first question in the array
let questionNumber = 0;
question.innerText = questions[questionNumber];
let points = 0;
let counter = 0;
submit.addEventListener("click", function(){
if(counter == questions.length - 1){
question.innerText = "You got " + points + " out of " + questions.length * 10 + " points";
answer.style.display = "none";
submit.style.display = "none";
}
answer = document.getElementById("answer").value;
if(answer == answers[questionNumber]){
points +=10;
console.log("correct");
}
else{
console.log("incorrect" + " " + "the correct answer is " + answers[questionNumber]);
if(points < 10){
points = 0;
}
else{
points -= 10;
}
}
//in the else I check if the user have more than 10 points and if so I decrement the points by 10 but id no then I set the points to 0
questionNumber++;
counter++;
question.innerText = questions[questionNumber];
});
<div class="container">
<span id="question"></span>
<div class="input-group flex-nowrap">
<input type="text" id="answer" class="form-control" placeholder="Answer Here..." aria-label="Username" aria-describedby="addon-wrapping">
</div>
<button type="button" id="submit" class="btn btn-outline-success">Submit</button>
</div>
【问题讨论】:
-
使变量不可见是什么意思?
-
我想更改它们的可见性,或者将显示更改为无,但它不起作用
-
变量没有可见性,DOM 元素有。
-
问题是你重用了变量
answer:answer = document.getElementById("answer").value;现在它包含了用户的答案而不是DOM元素,所以你不能让它不可见。 -
怎么样?你永远不会重用变量
submit,所以没关系。
标签: javascript html css