我正在为我的侄子们写《星球大战》测验。发布图片和问题后,我想清除屏幕并发布另一个图片和问题,但我似乎无法做到这一点。我要去哪里错了?这是我到目前为止的代码。它基本上一次打印两个问题。

<!doctype html>
<html>
<head>
    <title> Star Wars Quiz</title>
    <style>
        body {
            font-family:sans-serif;
            text-align:center;
        }
        form {
            margin:0 auto;
            width:500px;
           text-align:left;
        }
    </style>
 </head>
 <body>
<img src="http://i.imgur.com/xkdHqSM.jpg" width="500" height="500" />
    <form name="form1" method="post" action="">

        <p>What is this?</p>
        <p>
            <label><input type="radio" name="star" value="1"
                onclick="answer(this);">The DeathStar</label>
        </p>
        <p>
            <label><input type="radio" name="star" value="2"
                    onclick="answer(this);">Tatooine</label>
        </p>
        <p>
            <label><input type="radio" name="star" value="3"
                    onclick="answer(this);">Peppa Pigs House</label>
        </p>

    </form>
    <script>
        var globalScore = 0;

        function answer(selectedButton) {
            var score = document.getElementById("score");

            if (selectedButton.value == "1") {

                //document.body.innerHTML = "";//clear screen
                globalScore += 1;
            }



            score.value = globalScore;

        }
    </script>
    <p id="quest2">
    <img src="http://i.imgur.com/hee2oSS.png" width="500" height="500" />
         <form name="form2" method="post" action="">

        <p>What is this?</p>
        <p>
            <label><input type="radio" name="star" value="1"
                onclick="answer(this);">a Bow Fighter</label>
        </p>
        <p>
            <label><input type="radio" name="star" value="2"
                    onclick="answer(this);">a Tie Fighter</label>
        </p>
        <p>
            <label><input type="radio" name="star" value="3"
                    onclick="answer(this);">a Street Fighter</label>
        </p>

        </p>
    </form>

最佳答案

您可以使用此方法。.但是我认为您要遵循的方法差异很大,并且对您的项目而言效率不高。但我已经更正了您的代码。请检查。

<!doctype html>
<html>
<head>
    <title> Star Wars Quiz</title>
    <style>
        body {
            font-family:sans-serif;
            text-align:center;
        }
        form {
            margin:0 auto;
            width:500px;
       text-align:left;
    }


 </style>
 </head>
 <body>
<div id="score"></div>
<div id="quest1">
<img src="http://i.imgur.com/xkdHqSM.jpg" width="500" height="500" />
    <form name="form1" method="post" action="">

        <p>What is this?</p>
    <p>
        <label><input type="radio" name="star" value="1"
            onclick="answer(this);">The DeathStar</label>
    </p>
    <p>
        <label><input type="radio" name="star" value="2"
                onclick="answer(this);">Tatooine</label>
    </p>
    <p>
        <label><input type="radio" name="star" value="3"


                  onclick="answer(this);">Peppa Pigs House</label>
        </p>

    </form>
</div>
    <script>


          var globalScore = 0;

        function answer(selectedButton) {
            var score = document.getElementById("score");

            if (selectedButton.value == "1") {

                //document.body.innerHTML = "";//clear screen
                globalScore += 1;
            }


        score.innerHTML = globalScore;
    document.getElementById('quest1').style.display='none';
    document.getElementById('quest2').style.display='block';
    }
</script>
<div id="quest2" style="display:none;">
<img src="http://i.imgur.com/hee2oSS.png" width="500" height="500" />
     <form name="form2" method="post" action="">

    <p>What is this?</p>
    <p>
        <label><input type="radio" name="star" value="1"
            onclick="answer(this);">a Bow Fighter</label>
    </p>
    <p>
        <label><input type="radio" name="star" value="2"
                onclick="answer(this);">a Tie Fighter</label>
    </p>
    <p>


           <label><input type="radio" name="star" value="3"
                    onclick="answer(this);">a Street Fighter</label>
        </p>


    </form>
 </div>

09-17 18:40