猿问

如何通过单击按钮启动 html 表单,然后按钮消失并且表单出现在屏幕上?

我正在尝试制作一个测验网站,并希望它以“开始测验”按钮开始,然后消失并让位于第一个测验问题。到目前为止,我只有以下内容:


<html dir="ltr">

  <head>

    <meta charset="utf-8">

    <title>Quiz</title>

  </head>

  <body>

    <h1>Football Quiz Game</h1>

    <button type="button" name="mainButton" onClick = 

"this.style.visibility= 'hidden';">Begin Quiz!</button>


    <div class="firstQuestion">

    <h3>Who is the top all-time goalscorer in the UEFA Champions League? 

    </h3>

    <form class="question1">

      <input type="radio" id="Cristiano Ronaldo" name="topUCLscorer" 

value="Cristiano Ronaldo">

      <label for="topUCLscorer">Cristiano Ronaldo</label>

      <input type="radio" id="Raul" name="topUCLscorer" value="Raul">

      <label for="topUCLscorer">Raul</label>

      <input type="radio" id="Lionel Messi" name="topUCLscorer" 

value="Lionel Messi">

      <label for="topUCLscorer">Lionel Messi</label>

      <input type="radio" id="Karim Benzema" name="topUCLscorer" 

 value="Karim Benzema">

      <label for="topUCLscorer">Karim Benzema</label>

    </form>

    </div>

  </body>

</html>


一只甜甜圈
浏览 150回答 4
4回答

料青山看我应如是

我拿了代码并在codepen上更新了它。您可以在下面的链接中查看。希望它有效...&nbsp; &nbsp; <body>&nbsp; &nbsp; &nbsp; &nbsp; <h1>Football Quiz Game</h1>&nbsp; &nbsp; &nbsp; &nbsp; <p>Once you are ready to begin, please click the button below. Goodluck</p>&nbsp; &nbsp; &nbsp; &nbsp; <button type="button" name="mainButton" id="mainButton">Begin Quiz!</button>&nbsp; &nbsp; &nbsp; &nbsp; <div class="firstQuestion">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <h3>Who is the top all-time goalscorer in the UEFA Champions League?</h3>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <form class="question1">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <input type="radio" id="Cristiano Ronaldo" name="topUCLscorer"&nbsp;value="Cristiano Ronaldo">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <label for="topUCLscorer">Cristiano Ronaldo</label>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <input type="radio" id="Raul" name="topUCLscorer" value="Raul">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <label for="topUCLscorer">Raul</label>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <input type="radio" id="Lionel Messi" name="topUCLscorer"&nbsp;value="Lionel Messi">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <label for="topUCLscorer">Lionel Messi</label>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <input type="radio" id="Karim Benzema" name="topUCLscorer"&nbsp;&nbsp;value="Karim Benzema">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <label for="topUCLscorer">Karim Benzema</label>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; </form>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;</div>&nbsp; &nbsp; &nbsp;</body>CSS.firstQuestion {&nbsp; display: none;}JavaScriptconst firstQuestion = document.querySelector('.firstQuestion');const begin = document.querySelector('#mainButton');begin.addEventListener('click', (e) => {&nbsp; e.preventDefault();&nbsp; begin.style.display = 'none';&nbsp; firstQuestion.style.display = 'block';});试试这个:https ://codepen.io/dinakajoy/pen/eYJdqYx

肥皂起泡泡

您可以为此使用 JavaScript。尝试运行以下代码段:const quizButton = document.querySelector('#quizButton');const question = document.querySelector('.firstQuestion');const beginQuiz = () => {&nbsp; quizButton.style.visibility = 'hidden';&nbsp; question.style.visibility = 'visible';}<html dir="ltr">&nbsp; <head>&nbsp; &nbsp; <meta charset="utf-8">&nbsp; &nbsp; <title>Quiz</title>&nbsp; </head>&nbsp; <body>&nbsp; &nbsp; <h1>Football Quiz Game</h1>&nbsp; &nbsp; <button id="quizButton" type="button" name="mainButton" onClick =&nbsp;"beginQuiz()">Begin Quiz!</button>&nbsp; &nbsp; <div class="firstQuestion" style="visibility: hidden">&nbsp; &nbsp; <h3>Who is the top all-time goalscorer in the UEFA Champions League?&nbsp;&nbsp; &nbsp; </h3>&nbsp; &nbsp; <form class="question1" >&nbsp; &nbsp; &nbsp; <input type="radio" id="Cristiano Ronaldo" name="topUCLscorer"&nbsp;value="Cristiano Ronaldo">&nbsp; &nbsp; &nbsp; <label for="topUCLscorer">Cristiano Ronaldo</label>&nbsp; &nbsp; &nbsp; <input type="radio" id="Raul" name="topUCLscorer" value="Raul">&nbsp; &nbsp; &nbsp; <label for="topUCLscorer">Raul</label>&nbsp; &nbsp; &nbsp; <input type="radio" id="Lionel Messi" name="topUCLscorer"&nbsp;value="Lionel Messi">&nbsp; &nbsp; &nbsp; <label for="topUCLscorer">Lionel Messi</label>&nbsp; &nbsp; &nbsp; <input type="radio" id="Karim Benzema" name="topUCLscorer"&nbsp;&nbsp;value="Karim Benzema">&nbsp; &nbsp; &nbsp; <label for="topUCLscorer">Karim Benzema</label>&nbsp; &nbsp; </form>&nbsp; &nbsp; </div>&nbsp; </body></html>

ITMISS

我已经编辑了您自己的代码,并在最后添加了一个简单的 javascript 函数。我只更改了您的 onClick 功能<html dir="ltr">&nbsp; <head>&nbsp; &nbsp; <meta charset="utf-8">&nbsp; &nbsp; <title>Quiz</title>&nbsp; </head>&nbsp; <body>&nbsp; &nbsp; <h1>Football Quiz Game</h1>&nbsp; &nbsp; <button type="button" id="mainButton" onClick = "show_form()" >Begin Quiz!</button>&nbsp; &nbsp; <div class="firstQuestion">&nbsp; &nbsp; <h3>Who is the top all-time goalscorer in the UEFA Champions League?&nbsp;&nbsp; &nbsp; </h3>&nbsp; &nbsp; <form id="question1" style="visibility: hidden">&nbsp; &nbsp; &nbsp; <input type="radio" id="Cristiano Ronaldo" name="topUCLscorer"&nbsp;value="Cristiano Ronaldo">&nbsp; &nbsp; &nbsp; <label for="topUCLscorer">Cristiano Ronaldo</label>&nbsp; &nbsp; &nbsp; <input type="radio" id="Raul" name="topUCLscorer" value="Raul">&nbsp; &nbsp; &nbsp; <label for="topUCLscorer">Raul</label>&nbsp; &nbsp; &nbsp; <input type="radio" id="Lionel Messi" name="topUCLscorer"&nbsp;value="Lionel Messi">&nbsp; &nbsp; &nbsp; <label for="topUCLscorer">Lionel Messi</label>&nbsp; &nbsp; &nbsp; <input type="radio" id="Karim Benzema" name="topUCLscorer"&nbsp;&nbsp;value="Karim Benzema">&nbsp; &nbsp; &nbsp; <label for="topUCLscorer">Karim Benzema</label>&nbsp; &nbsp; </form>&nbsp; &nbsp; </div>&nbsp; &nbsp; <script>&nbsp; &nbsp; &nbsp; &nbsp; function show_form(){&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; document.getElementById("mainButton").style.visibility="hidden";&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; document.getElementById("question1").style.visibility="visible";&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; </script>&nbsp; </body></html>

斯蒂芬大帝

如果您使用的是 jQuery,请参阅下面的示例。$(function(){&nbsp;$(document).on("click", "#start", function(){&nbsp; $(this).hide();&nbsp; $(".firstQuestion").show();&nbsp;});});<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script><html dir="ltr">&nbsp; <head>&nbsp; &nbsp; <meta charset="utf-8">&nbsp; &nbsp; <title>Quiz</title>&nbsp; </head>&nbsp; <body>&nbsp; &nbsp; <h1>Football Quiz Game</h1>&nbsp; &nbsp; <button type="button" name="mainButton" id="start">Begin Quiz!</button>&nbsp; &nbsp; <div class="firstQuestion" style="display:none;">&nbsp; &nbsp; <h3>Who is the top all-time goalscorer in the UEFA Champions League?&nbsp;&nbsp; &nbsp; </h3>&nbsp; &nbsp; <form class="question1">&nbsp; &nbsp; &nbsp; <input type="radio" id="Cristiano Ronaldo" name="topUCLscorer"&nbsp;value="Cristiano Ronaldo">&nbsp; &nbsp; &nbsp; <label for="topUCLscorer">Cristiano Ronaldo</label>&nbsp; &nbsp; &nbsp; <input type="radio" id="Raul" name="topUCLscorer" value="Raul">&nbsp; &nbsp; &nbsp; <label for="topUCLscorer">Raul</label>&nbsp; &nbsp; &nbsp; <input type="radio" id="Lionel Messi" name="topUCLscorer"&nbsp;value="Lionel Messi">&nbsp; &nbsp; &nbsp; <label for="topUCLscorer">Lionel Messi</label>&nbsp; &nbsp; &nbsp; <input type="radio" id="Karim Benzema" name="topUCLscorer"&nbsp;&nbsp;value="Karim Benzema">&nbsp; &nbsp; &nbsp; <label for="topUCLscorer">Karim Benzema</label>&nbsp; &nbsp; </form>&nbsp; &nbsp; </div>&nbsp; </body></html>
随时随地看视频慕课网APP

相关分类

JavaScript
我要回答