无法弄清楚如何选择按钮

时间:2017-04-13 22:00:00

标签: javascript html css

我正在努力让你可以选择我的测验中的按钮,但我无法弄清楚如何。我已经设置了所以进入下一页的选项被隐藏,直到你选择了一些东西,但是,我今天大部分时间都在试图找到一种方法来实际选择其中一个按钮。我在我的css中分配了悬停和活动事件,但是在按下时保持选中该按钮时不知道该怎么做。

我们不允许使用像JQuery这样的外部库。 angularJS被认为是外部库吗?可能要问教授那个。

@import url(http://fonts.googleapis.com/css?family=Titillium+Web:900|Roboto:400,100);
body {
  background-color: black;
  padding: 20px;
}

#myQuiz {
  font-family: 'Roboto', sans-serif;
  font-size: 16px;
  font-weight: 400;
  width: 650px;
  650px;
  position: relative overflow: hidden;
  color: white;
  background: black;
}

#myQuiz h1 {
  font-weight: 100;
  font-size 2em;
  margin: 0px;
  position: absolute;
  top: 25px;
  left: 36px;
}

myQuiz h1 span {
  display: block;
  font-weight: 900;
  font-family: 'Titillium Web', sans- serif;
  font-size: 3.2 em;
  line-height: 65px;
}

#myQuiz h2 {
  font-size: 3em;
  margin: 0px;
  font-weight: 100;
}

#myQuiz h3 {
  font-size: 2em;
  margin: 0px;
  font-weight: 100;
}

#myQuiz p {
  margin: 0px 0px 14px 0px;
}

#myQuiz .btn {
  display: inline-block;
  cursor: pointer;
  background-color: #7E3517;
  color: white;
  text-decoration: none;
  padding: 5px 15px;
  border-radius: 6px;
}

#myQuiz .btn:hover {
  background-color: white;
  color: black;
  text-decoration: none;
  padding: 5px 15px;
  border-radius: 6px;
}

#myQuiz .btn:active {
  background-color: red;
  color: black;
  text-decoration: none;
  padding: 5px 15px;
  border-radius: 6px;
}

#myQuiz .progress {
  width: 550px;
  position: absolute;
  top: 160px;
  left: 40px;
}

#myQuiz .progress div {
  position: relative;
  display: inline-block;
  width: 30px;
  height: 30px;
  margin-right: 30px;
  border-radius: 50%;
  background-color: rgba(255, 255, 255, 0.2);
  transition: background-color 1s;
}

#myQuiz .progress div.on,
#myQuiz .progress div.answered {
  background-color: #ef0101;
}

#myQuiz .intro {
  position: absolute;
  top: 225px;
  left: 50px;
  width: 550px;
}

#myQuiz .intro p {
  margin: 0px 0px 40px 0px;
}

#myQuiz .question {
  width: 550px;
  position: absolute;
  top: 225px;
  left: 50px;
}

#myQuiz .question .text {
  font-size: 1.6em;
  margin: 0px, 0px, 20px, 0px;
}

#myQuiz .question .ans {
  display: inline-block;
  font-size: 1.1em;
  width: 225px;
  border: 2px solid red;
  border-radius: 6px;
  padding 10px;
  margin: 0px 15px 15px 0px;
}

#myQuiz .question .ans.selected {
  border-color: blue;
}

#myQuiz .question.unanswered .ans {
  cursor: pointer;
}

#myQuiz .question.unanswered .ans:hover {
  background-color: rgba(163, 111, 155, .2);
}

#myQuiz .question.answered .ans {
  cursor: default;
}

#myQuiz .done {
  color: yellow;
  margin-top: 50px;
  transition: opacity 1.5s, margin-top 1.5s;
  visibility: hidden;
  opacity: 0;
}

#myQuiz .done .btn {
  margin-top: 5px;
}

myQuiz .answered . done {
  visibility: visible;
  opacity: 1;
  margin-top: 10px;
}

#myQuiz .results {
  position: absolute;
  top: 225px;
  left: 660px;
  width: 550px;
}
<div id="myQuiz">
  <h1>Spirit Animal Quiz</h1>
  <div class="progress">
    <div class="on"></div>
    <div></div>
    <div></div>
    <div></div>
    <div></div>
    <div></div>
    <div></div>
  </div>

  <div class="question">
    <form action="">
      <p> <input type="radio" class="btn" value="male"> Rough<br></p>
      <p><input type="radio" class="btn" value="female"> Soft<br></p>
      <p> <input type="radio" class="btn" value="other"> both </p>
    </form>
    <p class="text">How would you describe your skin?</p>
    <p class="btn">Rough</p>
    <p class="btn">Smooth</p>
    <p class="btn">Both</p>
    <div class=d one>
      <div class="btn">Next</div>
    </div>
  </div>
</div>
    

1 个答案:

答案 0 :(得分:0)

您可以使用css属性display来显示和隐藏您的下一个按钮。 使用:display: none;隐藏元素display: inline;以恢复默认值 我给了&#34; nxt&#34;的按钮和ID。为了方便访问,并为所有按钮添加了事件监听器(因为我不知道在显示下一个之前你想要按哪个按钮)使用onClick事件,您可以执行在回调函数中单击后应该发生的任何事情。

&#13;
&#13;
var btns = document.getElementsByClassName("btn");
for( var i =0; i < btns.length; i++){
    btns[i].addEventListener("click", function(){
         document.getElementById("nxt").style.display = "inline";
    });
}
&#13;
#nxt{
    display: none;
}
#nxt, p.btn{
    background-color: black;
    color: white;
    width : 150px;
    height: 40px;
    text-align: center;
}
&#13;
<head>
    <meta content="text/html; charset=UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <title>Find Out Your Spirit Animal</title>
    <link rel="stylesheet" type="text/css" href="quiz.css">
</head>
<body>
    <div id= "myQuiz">
        <h1>Spirit Animal Quiz</h1>
        <div class = "progress">
        <div class="on"></div>
        <div></div>
        <div></div>   
        <div></div>
        <div></div>
        <div></div>
        <div></div>
        </div>

        <div class = "question">
            <form action="">
               <p> <input type="radio" class= "btn" value="male"> Rough<br></p>
                <p><input type="radio" class="btn" value="female"> Soft<br></p>
                <p> <input type="radio" class="btn" value="other"> both </p>
            </form>
            <p class = "text">How would you describe your skin?</p>
            <p class = "btn">Rough</p>
            <p class = "btn">Smooth</p>
            <p class = "btn">Both</p>
        <div class = done>
        <div class="btn" id="nxt">Next</div>
        </div>
    </div>
    </div>
</body>
&#13;
&#13;
&#13;