2017-01-10 7 views
0

以前の質問に応じて、質問と回答のクイズを作成しようとしています。したがって、ユーザーが「はい」をクリックすると、前の質問が画面から消え、新しい質問が画面に表示されます。何もクリックされていなければ、別の質問が表示されます。どんな助けも大歓迎です。ありがとうございました。以前の質問による多レベルの回答

コード:

<!DOCTYPE html> 
<html> 

<body> 

<p>Is the answer to this question Yes or No?</p> 
<button onclick="myFunction1()">Yes</button> 
<button onclick="myFunction2()">No</button> 
<p id="demo"></p> 

<script> 
function myFunction1() { 
} 
function myFunction1() { 
} 

</script> 
</body> 
</html> 

答えて

0

それを行う最も簡単な方法は、次のように表示CSSプロパティを使用することです:

<!DOCTYPE html> 
 
<html> 
 

 
    <body> 
 
    <div id="first-question"> 
 
     <p>Is the answer to this question Yes or No?</p> 
 
     <button onclick="myFunction('first-question', 'second-question-1')">Yes</button> 
 
     <button onclick="myFunction('first-question', 'second-question-2')">No</button> 
 
    </div> 
 
    <div id="second-question-1" style="display: none;"> 
 
     <p>You have clicked "Yes"</p> 
 
     <p>Return to the first question ?</p> 
 
     <button onclick="myFunction('second-question-1', 'first-question')">Yes</button> 
 
     <button onclick="">No</button> 
 
    </div> 
 
    <div id="second-question-2" style="display: none;"> 
 
     <p>You have clicked "No"</p> 
 
    <p>Return to the first question ?</p> 
 
     <button onclick="myFunction('second-question-2', 'first-question')">Yes</button> 
 
     <button onclick="">No</button> 
 
    </div> 
 

 

 
    <script type="text/javascript"> 
 
     function myFunction (hide, show) { 
 
     document.getElementById(hide).style.display = "none"; 
 
     document.getElementById(show).style.display = "block"; 
 
     } 
 

 
    </script> 
 
    </body> 
 

 
</html>

UPDATE:のみ1機能を使用

+0

これは私が探しているものとまったく同じように見えますが、何5つのレベルの質問が必要な場合はどうすればいいですか?質問の数は2^n(nはレベル)になりますか? – sphe2011

+0

まあ...多くのdiv要素を持ち、多くのidを持つでしょう。 idをパラメータで送信することで、js関数を1つだけ使用できます。 – AntoineCa

+0

function myFunction3(){ \t document.getElementById( "first-question")。style.display = "none"; \t document.getElementById( "second-question-2")。style.display = "none"; document.getElementById( "three-question-1")。style.display = "block"; } まだ2番目の質問が表示されます。 – sphe2011

関連する問題