如何一次随机显示三个DIV?
我正在尝试使用三种不同的方法来创建一个测验应用程序,以回答问题,可以通过使用无线电,文本输入或下拉来回答问题。我创建了三个< div>
每个都有不同的答案方法,我如何在我的页面上随机显示其中的一个< div>
?
const userName = document.getElementById("user-name");
const submitUserName = document.getElementById("submitUserName");
const nav = document.querySelector("a");
submitUserName.onclick = function() {
if (userName.value == "") {
alert("Kindly Input your Name !")
} else {
nav.setAttribute("href", "/quiz/index.html")
}
}
const quizQuestions = [
{
question: "Which language runs in a web browser ?",
a: "Python",
b: "Javascript",
c: "Java",
d: "Flutter",
ans: b,
},
{
question: "How many standard color names does HTML supports ?",
a: "20",
b: "90",
c: "140",
d: "125",
ans: c,
},
{
question: "Javascript displays numbers as base ?",
a: "10",
b: "2",
c: "5",
d: "12",
ans: a,
},
{
question: "Who is the father of computer ?",
a: "Charles Babbage",
b: "Allan Turing",
c: "Issac Newton",
d: "Bill Gate",
ans: a,
},
{
question: "The brain of a computer is ?",
a: "ALU",
b: "DVD",
c: "CPU",
d: "Memory",
ans: c,
},
{
question: "What year was JavaScript launched ?",
a: "1996",
b: "1995",
c: "1994",
d: "1993",
ans: b,
},
{
question: "Junk e-mail is called ?",
a: "junk",
b: "spoof",
c: "sperm",
d: "spam",
ans: d,
},
{
question: "Which tag is used for list-items ?",
a: "<ol>",
b: "<ul>",
c: "<dl>",
d: "<li>",
ans: d,
},
{
question: "How old is Bill Gate ?",
a: "59",
b: "60",
c: "56",
d: "66",
ans: d,
},
{
question: "Which opetator is used to assign a value to a variable ?",
a: "=",
b: "&",
c: "%",
d: "$",
ans: a,
},
{
question: "How do you declare a JavaScript variable ?",
a: "variable myVar",
b: "v myVar",
c: "var myVar",
d: "vrb myVar",
ans: c,
},
];
@import url(https://fonts.googleapis.com/css?family=Poppins:100,200,300,regular,500,600,700,800,900);
:root {
--background: rgb(29, 26, 26);
--text-color: rgb(255, 255, 255);
}
* {
box-sizing: border-box;
margin: 0;
padding: 0;
}
h3, h4 {
color: var(--text-color);
}
body {
background-color: var(--background);
font-family: 'Poppins', sans-serif;
}
.quiz-container {
margin: 30px auto;
width: 70%;
text-align: center;
display: flex;
flex-direction: column;
gap: 35px;
}
.header h3 {
color: var(--text-color);
font-size: 30px;
}
.questions {
display: flex;
flex-direction: column;
gap: 20px;
}
.question-no {
margin-bottom: 15px;
}
.question-options {
display: flex;
flex-direction: column;
gap: 25px;
width: 100%;
margin: 0 auto;
}
select {
padding: 8px;
outline: none;
border: none;
font-size: 15px;
cursor: pointer;
width: 50%;
}
option{
cursor: pointer;
}
.question-tog {
display: flex;
justify-content: space-between;
align-items: center;
width: 50%;
margin: 0 auto;
}
.drop-down, .radio, .text-input {
width: 100%;
}
.radio {
display: flex;
flex-direction: column;
align-items: center;
gap: 10px;
}
.radio-container {
display: flex;
gap: 10px;
width: 50%;
background-color: white;
padding: 8px;
border-radius: 5px;
}
.radio-container:hover {
cursor: pointer;
box-shadow: 0 0.4rem 1.4rem 0 black;
transform: scale(1.05);
transition: transform 100ms;
}
.radio-container input[type="radio"] {
margin-left: 10px;
cursor: pointer;
}
.text-input input[type="text"] {
width: 50%;
padding: 10px;
outline: none;
font-size: 15px;
border-radius: 5px;
border: none;
}
button {
padding: 8px 35px;
cursor: pointer;
border-radius: 5px;
font-size: 15px;
display: flex;
align-items: center;
gap: 20px;
}
i {
font-size: 25px;
}
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Interactive Quiz App</title>
<link href='https://unpkg.com/[email protected]/css/boxicons.min.css' rel='stylesheet'>
<link rel="stylesheet" href="/quiz/style.css">
</head>
<body>
<!-- Quiz Container -->
<div class="quiz-container">
<div class="header"><h3>Interactive Quiz App</h3></div>
<div class="quiz">
<div class="question-no"><h3>Question 1 of 5</h3></div>
<div class="questions">
<div class="question-text">
<h4 id="question">When is 2 * 18 ?</h4>
</div>
<div class="question-options">
<!-- Drop Down -->
<div class="drop-down" id="drop-down">
<select name="format" id="format">
<option selected disabled>Choose your answer</option>
<option value="1">32</option>
<option value="2">36</option>
<option value="3">62</option>
<option value="4">22</option>
</select>
</div>
<!-- Radio -->
<div class="radio" id="radio">
<label class="radio-container" id="label1">
<input type="radio" name="radioQuiz" id="1" value="1">
<span class="mqs">Hello</span>
</label>
<label class="radio-container" id="label2">
<input type="radio" name="radioQuiz" id="2" value="2">
<span class="mqs">Hello</span>
</label>
<label class="radio-container" id="label3">
<input type="radio" name="radioQuiz" id="3" value="3">
<span class="mqs">Hello</span>
</label>
<label class="radio-container" id="label4">
<input type="radio" name="radioQuiz" id="4" value="4">
<span class="mqs">Hello</span>
</label>
</div>
<!-- Text Input -->
<div class="text-input" id="text-input">
<input type="text" name="text-input" id="text-answer">
</div>
</div>
</div>
</div>
<!-- Question Next and Previous -->
<div class="question-tog">
<button class="prev"> <span>Prev</span> <i class='bx bx-left-arrow-alt'></i></button>
<button class="next"> <span>Next</span> <i class='bx bx-right-arrow-alt'></i></button>
</div>
</div>
<script src="/quiz/main.js"></script>
</body>
</html>
我如何一次仅显示此div
一次随机显示一个,当我单击下一个按钮时,它应该给出另一种随机答案方法。
I am trying to create a quiz app with three different ways to answer the questions, the questions can be answered by using either a radio, text-input or drop-down. I created three <div>
each having the different answering methods, how can I display just one of those <div>
randomly on my page ?
const userName = document.getElementById("user-name");
const submitUserName = document.getElementById("submitUserName");
const nav = document.querySelector("a");
submitUserName.onclick = function() {
if (userName.value == "") {
alert("Kindly Input your Name !")
} else {
nav.setAttribute("href", "/quiz/index.html")
}
}
const quizQuestions = [
{
question: "Which language runs in a web browser ?",
a: "Python",
b: "Javascript",
c: "Java",
d: "Flutter",
ans: b,
},
{
question: "How many standard color names does HTML supports ?",
a: "20",
b: "90",
c: "140",
d: "125",
ans: c,
},
{
question: "Javascript displays numbers as base ?",
a: "10",
b: "2",
c: "5",
d: "12",
ans: a,
},
{
question: "Who is the father of computer ?",
a: "Charles Babbage",
b: "Allan Turing",
c: "Issac Newton",
d: "Bill Gate",
ans: a,
},
{
question: "The brain of a computer is ?",
a: "ALU",
b: "DVD",
c: "CPU",
d: "Memory",
ans: c,
},
{
question: "What year was JavaScript launched ?",
a: "1996",
b: "1995",
c: "1994",
d: "1993",
ans: b,
},
{
question: "Junk e-mail is called ?",
a: "junk",
b: "spoof",
c: "sperm",
d: "spam",
ans: d,
},
{
question: "Which tag is used for list-items ?",
a: "<ol>",
b: "<ul>",
c: "<dl>",
d: "<li>",
ans: d,
},
{
question: "How old is Bill Gate ?",
a: "59",
b: "60",
c: "56",
d: "66",
ans: d,
},
{
question: "Which opetator is used to assign a value to a variable ?",
a: "=",
b: "&",
c: "%",
d: "$",
ans: a,
},
{
question: "How do you declare a JavaScript variable ?",
a: "variable myVar",
b: "v myVar",
c: "var myVar",
d: "vrb myVar",
ans: c,
},
];
@import url(https://fonts.googleapis.com/css?family=Poppins:100,200,300,regular,500,600,700,800,900);
:root {
--background: rgb(29, 26, 26);
--text-color: rgb(255, 255, 255);
}
* {
box-sizing: border-box;
margin: 0;
padding: 0;
}
h3, h4 {
color: var(--text-color);
}
body {
background-color: var(--background);
font-family: 'Poppins', sans-serif;
}
.quiz-container {
margin: 30px auto;
width: 70%;
text-align: center;
display: flex;
flex-direction: column;
gap: 35px;
}
.header h3 {
color: var(--text-color);
font-size: 30px;
}
.questions {
display: flex;
flex-direction: column;
gap: 20px;
}
.question-no {
margin-bottom: 15px;
}
.question-options {
display: flex;
flex-direction: column;
gap: 25px;
width: 100%;
margin: 0 auto;
}
select {
padding: 8px;
outline: none;
border: none;
font-size: 15px;
cursor: pointer;
width: 50%;
}
option{
cursor: pointer;
}
.question-tog {
display: flex;
justify-content: space-between;
align-items: center;
width: 50%;
margin: 0 auto;
}
.drop-down, .radio, .text-input {
width: 100%;
}
.radio {
display: flex;
flex-direction: column;
align-items: center;
gap: 10px;
}
.radio-container {
display: flex;
gap: 10px;
width: 50%;
background-color: white;
padding: 8px;
border-radius: 5px;
}
.radio-container:hover {
cursor: pointer;
box-shadow: 0 0.4rem 1.4rem 0 black;
transform: scale(1.05);
transition: transform 100ms;
}
.radio-container input[type="radio"] {
margin-left: 10px;
cursor: pointer;
}
.text-input input[type="text"] {
width: 50%;
padding: 10px;
outline: none;
font-size: 15px;
border-radius: 5px;
border: none;
}
button {
padding: 8px 35px;
cursor: pointer;
border-radius: 5px;
font-size: 15px;
display: flex;
align-items: center;
gap: 20px;
}
i {
font-size: 25px;
}
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Interactive Quiz App</title>
<link href='https://unpkg.com/[email protected]/css/boxicons.min.css' rel='stylesheet'>
<link rel="stylesheet" href="/quiz/style.css">
</head>
<body>
<!-- Quiz Container -->
<div class="quiz-container">
<div class="header"><h3>Interactive Quiz App</h3></div>
<div class="quiz">
<div class="question-no"><h3>Question 1 of 5</h3></div>
<div class="questions">
<div class="question-text">
<h4 id="question">When is 2 * 18 ?</h4>
</div>
<div class="question-options">
<!-- Drop Down -->
<div class="drop-down" id="drop-down">
<select name="format" id="format">
<option selected disabled>Choose your answer</option>
<option value="1">32</option>
<option value="2">36</option>
<option value="3">62</option>
<option value="4">22</option>
</select>
</div>
<!-- Radio -->
<div class="radio" id="radio">
<label class="radio-container" id="label1">
<input type="radio" name="radioQuiz" id="1" value="1">
<span class="mqs">Hello</span>
</label>
<label class="radio-container" id="label2">
<input type="radio" name="radioQuiz" id="2" value="2">
<span class="mqs">Hello</span>
</label>
<label class="radio-container" id="label3">
<input type="radio" name="radioQuiz" id="3" value="3">
<span class="mqs">Hello</span>
</label>
<label class="radio-container" id="label4">
<input type="radio" name="radioQuiz" id="4" value="4">
<span class="mqs">Hello</span>
</label>
</div>
<!-- Text Input -->
<div class="text-input" id="text-input">
<input type="text" name="text-input" id="text-answer">
</div>
</div>
</div>
</div>
<!-- Question Next and Previous -->
<div class="question-tog">
<button class="prev"> <span>Prev</span> <i class='bx bx-left-arrow-alt'></i></button>
<button class="next"> <span>Next</span> <i class='bx bx-right-arrow-alt'></i></button>
</div>
</div>
<script src="/quiz/main.js"></script>
</body>
</html>
How can I display just one of this div
randomly one at a time and when I click on the next button it should give another random answering method.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您可以尝试此解决方案。
html代码:
JS代码
you may try this solution.
HTML code:
JS code
这应该有效
编辑:我编辑它在时间
html中仅显示一个:
this should work
Edit: I editet it that it shows only one at time
HTML: