Javascript 如何在测验中一次显示一个元素
我有一个非常简单的测试,问用户一些关于他们应该买什么样的车的选择题 我如何一次只展示一个问题?例如添加“下一步”和“上一步”按钮。我仍然是Javascript的初学者,我找不到这个问题的答案Javascript 如何在测验中一次显示一个元素,javascript,Javascript,我有一个非常简单的测试,问用户一些关于他们应该买什么样的车的选择题 我如何一次只展示一个问题?例如添加“下一步”和“上一步”按钮。我仍然是Javascript的初学者,我找不到这个问题的答案 有几种方法可以解决这个问题。我试图解释一个解决方案,但我不能给你一个完整的脚本解决方案,因为我不认为这是StackOverflow的范围 您可以做的第一件事是将不同的问题分组到html标记中。通常,div标签是一个不错的选择,例如: <div id="question1"> <p&g
有几种方法可以解决这个问题。我试图解释一个解决方案,但我不能给你一个完整的脚本解决方案,因为我不认为这是StackOverflow的范围 您可以做的第一件事是将不同的问题分组到html标记中。通常,
div
标签是一个不错的选择,例如:
<div id="question1">
<p>What size do you need?</p>
<input type="radio" name="size" value="compact">Compact<br>
<input type="radio" name="size" value="mid">Midsize<br>
<input type="radio" name="size" value="small">Small SUV<br>
<input type="radio" name="size" value="med">Medium SUV<br>
<input type="radio" name="size" value="big">Big SUV
</div>
如果“下一步”按钮的id为“下一步”,请为单击事件添加事件处理程序:
// JS
var nextButton = document.getElementById('next');
nextButton.onClick = function() {
// Increment the current question
question++;
if (question > MAX_QUESTIONS) {
// Hide the button
nextButton.className += " hidden";
}
// Hide the last question
document.getElementById('question' + (question - 1)).className += " hidden";
// Show the current question
document.getElementById('question' + question).className =
document.getElementById('question' + question).className.replace("hidden", "");
}
等等。我想你会自己解决剩下的问题
记住,这是纯JS。您还可以使用一些使开始学习JS更容易的库,例如jQuery
要从dom元素中添加和删除类,还可以实现此处描述的功能:有几种方法可以管理这些功能。我试图解释一个解决方案,但我不能给你一个完整的脚本解决方案,因为我不认为这是StackOverflow的范围 您可以做的第一件事是将不同的问题分组到html标记中。通常,
div
标签是一个不错的选择,例如:
<div id="question1">
<p>What size do you need?</p>
<input type="radio" name="size" value="compact">Compact<br>
<input type="radio" name="size" value="mid">Midsize<br>
<input type="radio" name="size" value="small">Small SUV<br>
<input type="radio" name="size" value="med">Medium SUV<br>
<input type="radio" name="size" value="big">Big SUV
</div>
如果“下一步”按钮的id为“下一步”,请为单击事件添加事件处理程序:
// JS
var nextButton = document.getElementById('next');
nextButton.onClick = function() {
// Increment the current question
question++;
if (question > MAX_QUESTIONS) {
// Hide the button
nextButton.className += " hidden";
}
// Hide the last question
document.getElementById('question' + (question - 1)).className += " hidden";
// Show the current question
document.getElementById('question' + question).className =
document.getElementById('question' + question).className.replace("hidden", "");
}
等等。我想你会自己解决剩下的问题
记住,这是纯JS。您还可以使用一些使开始学习JS更容易的库,例如jQuery
要从dom元素中添加和删除类,还可以实现以下函数:
// JS
var question = 1;
// JS
var nextButton = document.getElementById('next');
nextButton.onClick = function() {
// Increment the current question
question++;
if (question > MAX_QUESTIONS) {
// Hide the button
nextButton.className += " hidden";
}
// Hide the last question
document.getElementById('question' + (question - 1)).className += " hidden";
// Show the current question
document.getElementById('question' + question).className =
document.getElementById('question' + question).className.replace("hidden", "");
}