2017-12-12 13 views
-1

現在、各タブに1つの質問を表示するスキルテストプロジェクトに取り組んでいます。どのようにデフォルトのアクティブなタブを設定できますか?私はフォームw3schools.comをコピーした既定の開いているタブのコードがありますが、それは動作しません。なぜ私は考えていない。私は誰かが私のコードを修正するのを手伝ってくれることを願っています。どうもありがとうございます!htmlの既定のアクティブな/開いているタブを設定する方法

スタイル:

.button { 
    display: inline-block; 
    margin: 4px 2px; 
    border-radius: 8px; 
} 

div.tab { 
    overflow: hidden; 
    border: 1px solid #ccc; 
    background-color: #f1f1f1; 
} 

div.tab button { 
    background-color: inherit; 
    float: left; 
    border: 1px solid #ccc; 
    outline: none; 
    cursor: pointer; 
    padding: 13px 12.62px; 
    transition: 0.3s; 
    font-size: 10px; 
} 

div.tab button:hover { 
    background-color: #ddd; 
} 

div.tab button.active { 
    background-color: #d6f5d6; 
} 

.tabcontent { 
    display: none; 
    border: 1px solid #ccc; 
    border-top: none; 
} 

ボディ:

<div class="col-md-auto col-md-offset-1 col-centered"> 
    <div class="panel panel-success"> 
     <script> 
     function openTab(evt, tabName) { 
      var i, tabcontent, tablinks; 
      tabcontent = document.getElementsByClassName("tabcontent"); 
      for (i = 0; i < tabcontent.length; i++) { 
       tabcontent[i].style.display = "none"; 
      } 
      tablinks = document.getElementsByClassName("tablinks"); 
      for (i = 0; i < tablinks.length; i++) { 
       tablinks[i].className = tablinks[i].className.replace(" active", ""); 
      } 
      document.getElementById(tabName).style.display = "block"; 
      evt.currentTarget.className += " active"; 
     } 
     document.getElementById("defaultOpen").click(); 

     </script> 

     <div class="tab"> 
      <button class="tablinks" type="button" onclick="openTab(event, 'q1')" id="defaultOpen"></button> 
      <button class="tablinks" type="button" onclick="openTab(event, 'q2')"></button> 
      <button class="tablinks" type="button" onclick="openTab(event, 'q3')"></button>   

     <?php if (mysqli_num_rows($result) > 0): ?> 
     <?php $index = 0; $num=0; ?> 
       <div id="q<?php echo ($index++); ?>" class="tabcontent"> 
        <table class="table table-hover"> 
        <tbody> 
         <tr class="form-group"> 
          <h3 name="ques[<?php echo $test_id;?>]" style="text-indent: 40px;"> <?php echo $num,'. ', $question; ?> </h3> 
         </tr> 
         <tr class="form-group"> 
          <label class="radio-inline" style="text-indent: 70px; font-size: 18px;"> 
           &nbsp;&nbsp;<input style="font-size: 18px;" type="radio" name="ans[<?php echo $test_id;?>]" value="<?php echo $optiona;?>"><?php echo $optiona;?> 
          </label> 
          <br> 
          <label class="radio-inline" style="text-indent: 70px; font-size: 18px;"> 
           &nbsp;&nbsp;<input style="font-size: 18px;" type="radio" name="ans[<?php echo $test_id;?>]" value="<?php echo $optionb;?>"><?php echo $optionb; ?> 
          </label> 
          <br> 
          <label class="radio-inline" style="text-indent: 70px; font-size: 18px;"> 
           &nbsp;&nbsp;<input style="font-size: 18px;" type="radio" name="ans[<?php echo $test_id;?>]" value="<?php echo $optionc;?>"><?php echo $optionc;?> 
          </label> 
          <br> 
         </tr> 
        </tbody> 
        </table> 
        <br> 
       </div> 
      <?php $num++; ?> 
     <?php endif ?> 
     </form> 
    </div> 
    </div> 
</div> 
+4

私はPHPを知らないが、あなたはありませんforeachのとendforeachを持っています。 –

+0

ああ、私はそれらが重要であるか私の質問に関係していないので、私のコードのいくつかの部分を含んでいませんでした。 – Dza

答えて

0

は、私はちょうど赤に緑とアクティブにホバーの色を変更しました。あなたが最初のボタンをアクティブにするために参照している場合は、単にこれに変更します。

<button class="tablinks active" type="button" onclick="openTab(event, 'q1')" id="defaultOpen"></button> 

私たちはクラスで「active」を追加しました。

<body onload="document.getElementById('defaultOpen').click();"> 

は、我々は何をしている:そしてへのあなたの体のタグを変更?
デフォルトのボタンをアクティブにしていて、コンテンツがロードされていることを確認するために を作成しています。 はデフォルトボタンのonClickを呼び出してコンテンツを表示しています。

.button { 
 
    display: inline-block; 
 
    margin: 4px 2px; 
 
    border-radius: 8px; 
 
} 
 

 
div.tab { 
 
    overflow: hidden; 
 
    border: 1px solid #ccc; 
 
    background-color: #f1f1f1; 
 
} 
 

 
div.tab button { 
 
    background-color: inherit; 
 
    float: left; 
 
    border: 1px solid #ccc; 
 
    outline: none; 
 
    cursor: pointer; 
 
    padding: 13px 12.62px; 
 
    transition: 0.3s; 
 
    font-size: 10px; 
 
} 
 

 
div.tab button:hover { 
 
    background-color: green; 
 
} 
 

 
div.tab button.active { 
 
    background-color: red; 
 
} 
 

 
.tabcontent { 
 
    display: none; 
 
    border: 1px solid #ccc; 
 
    border-top: none; 
 
}
<body onload="document.getElementById('defaultOpen').click();"> 
 
<div class="col-md-auto col-md-offset-1 col-centered"> 
 
    <div class="panel panel-success"> 
 
    <script> 
 
     function openTab(evt, tabName) { 
 
     var i, tabcontent, tablinks; 
 
     tabcontent = document.getElementsByClassName("tabcontent"); 
 
     for (i = 0; i < tabcontent.length; i++) { 
 
      tabcontent[i].style.display = "none"; 
 
     } 
 
     tablinks = document.getElementsByClassName("tablinks"); 
 
     for (i = 0; i < tablinks.length; i++) { 
 
      tablinks[i].className = tablinks[i].className.replace(" active", ""); 
 
     } 
 
     document.getElementById(tabName).style.display = "block"; 
 
     evt.currentTarget.className += " active"; 
 
     } 
 
     document.getElementById("defaultOpen").click(); 
 
    </script> 
 

 
    <div class="tab"> 
 
     <button class="tablinks active" type="button" onclick="openTab(event, 'q1')" id="defaultOpen"></button> 
 
     <button class="tablinks" type="button" onclick="openTab(event, 'q2')"></button> 
 
     <button class="tablinks" type="button" onclick="openTab(event, 'q3')"></button> 
 

 
     <?php if (mysqli_num_rows($result) > 0): ?> 
 
     <?php $index = 0; $num=0; ?> 
 
     <div id="q1" class="tabcontent"> 
 
     <table class="table table-hover"> 
 
      <tbody> 
 
      <tr class="form-group"> 
 
       <h3 name="ques1" style="text-indent: 40px;"> 
 
       <?php echo $num,'. ', $question; ?> </h3> 
 
      </tr> 
 
      <tr class="form-group"> 
 
       <label class="radio-inline" style="text-indent: 70px; font-size: 18px;"> 
 
           &nbsp;&nbsp;<input style="font-size: 18px;" type="radio" name="ans1" value="1">Option1 
 
          </label> 
 
       <br> 
 
       <label class="radio-inline" style="text-indent: 70px; font-size: 18px;"> 
 
           &nbsp;&nbsp;<input style="font-size: 18px;" type="radio" name="ans2" value="2">Option 2 
 
          </label> 
 
       <br> 
 
       <label class="radio-inline" style="text-indent: 70px; font-size: 18px;"> 
 
           &nbsp;&nbsp;<input style="font-size: 18px;" type="radio" name="ans3" value="3">Option 3 
 
          </label> 
 
       <br> 
 
      </tr> 
 
      </tbody> 
 
     </table> 
 
     <br> 
 
     </div> 
 
     <?php $num++; ?> 
 
     <?php endforeach ?> 
 
     <?php endif ?> 
 
     </form> 
 
    </div> 
 
    </div> 
 
</div>

+0

私はあなたが提案した変更を試してみました。どうもありがとうございます!! – Dza

+0

@Dza問題はありません:) –

関連する問題