【问题标题】:Nested tabs in bootstrap引导程序中的嵌套选项卡
【发布时间】:2013-10-27 21:39:19
【问题描述】:

我在 Bootstrap 中有嵌套选项卡,代码如下:http://jsfiddle.net/RLdYC/

$("ul.nav-tabs a").click(function (e) {
  e.preventDefault();
  $(this).tab('show');
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>
<link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" rel="stylesheet"/>



<div class="tabbable boxed parentTabs">
    <ul class="nav nav-tabs">
        <li class="active"><a href="#set1">Tab 1</a>
        </li>
        <li><a href="#set2">Tab 2</a>
        </li>
    </ul>
    <div id="myTabContent" class="tab-content">
        <div class="tab-pane fade active in" id="set1">
            <div class="tabbable">
                <ul class="nav nav-tabs">
                    <li class="active"><a href="#sub11">Tab 1.1</a>
                    </li>
                    <li><a href="#sub12">Tab 1.2</a>
                    </li>
                </ul>
                <div id="myTabContent" class="tab-content">
                    <div class="tab-pane fade active in" id="sub11">
                        <p>Tab 1.1</p>
                    </div>
                    <div class="tab-pane fade" id="sub12">
                        <p>Tab 1.2</p>
                    </div>
                </div>
            </div>
        </div>
        <div class="tab-pane fade" id="set2">
            <div class="tabbable">
                <ul class="nav nav-tabs">
                    <li class="active"><a href="#sub21">Tab 2.1</a>
                    </li>
                    <li><a href="#sub22">Tab 2.2</a>
                    </li>
                </ul>
                <div id="myTabContent" class="tab-content">
                    <div class="tab-pane fade active in" id="sub21">
                        <p>Tab 2.1</p>
                    </div>
                    <div class="tab-pane fade" id="sub22">
                        <p>Tab 2.2</p>
                    </div>
                </div>
            </div>
        </div>
    </div>
</div>

问题是,一旦您单击顶行的不同选项卡,下面嵌套选项卡的实际内容将被隐藏。我需要显示第一个子选项卡的内容。

我尝试在第一个子选项卡上触发 click() 函数,但没有帮助。你能写出我需要解决这个问题的代码吗?

【问题讨论】:

    标签: javascript jquery twitter-bootstrap


    【解决方案1】:

    您有多个 id 为 myTabContent 的元素。

    HTML id 在整个页面中应该是唯一的。

    只需删除这些 id 或改用类 (working demo here)。

    $("ul.nav-tabs a").click(function (e) {
      e.preventDefault();
      $(this).tab('show');
    });
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
    <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>
    <link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" rel="stylesheet"/>
    
    
    <div class="tabbable boxed parentTabs">
        <ul class="nav nav-tabs">
            <li class="active"><a href="#set1">Tab 1</a>
            </li>
            <li><a href="#set2">Tab 2</a>
            </li>
        </ul>
        <div class="tab-content">
            <div class="tab-pane fade active in" id="set1">
                <div class="tabbable">
                    <ul class="nav nav-tabs">
                        <li class="active"><a href="#sub11">Tab 1.1</a>
                        </li>
                        <li><a href="#sub12">Tab 1.2</a>
                        </li>
                    </ul>
                    <div class="tab-content">
                        <div class="tab-pane fade active in" id="sub11">
                            <p>Tab 1.1</p>
                        </div>
                        <div class="tab-pane fade" id="sub12">
                            <p>Tab 1.2</p>
                        </div>
                    </div>
                </div>
            </div>
            <div class="tab-pane fade" id="set2">
                <div class="tabbable">
                    <ul class="nav nav-tabs">
                        <li class="active"><a href="#sub21">Tab 2.1</a>
                        </li>
                        <li><a href="#sub22">Tab 2.2</a>
                        </li>
                    </ul>
                    <div class="tab-content">
                        <div class="tab-pane fade active in" id="sub21">
                            <p>Tab 2.1</p>
                        </div>
                        <div class="tab-pane fade" id="sub22">
                            <p>Tab 2.2</p>
                        </div>
                    </div>
                </div>
            </div>
        </div>
    </div>

    【讨论】:

    • 我的 ID 是独一无二的,但指向工作演示的链接正是我所需要的,谢谢!
    【解决方案2】:

    试试这个:

    <div class="row">
            <div class="col-md-12">
             <!-- Custom Tabs -->
              <div class="nav-tabs-custom">
              <ul class="nav nav-tabs">
            <li class="active"><a href="#set1">New</a>
            </li>
            <li><a href="#set2">Confirmed</a>
            </li>
        </ul>
        <div class="tab-content">
    
            <div class="tab-pane fade active in" id="set1">
    
                <div class="tabbable">
                     <ul class="nav nav-tabs">
                  <li class="active"><a href="#tab_1" data-toggle="tab" aria-expanded="true">Web Design and Development</a></li>
                  <li class=""><a href="#tab_2" data-toggle="tab" aria-expanded="false">Domain Registration</a></li>
                  <li class=""><a href="#tab_3" data-toggle="tab" aria-expanded="false">Cloud Computing Services</a></li>
                  <li class=""><a href="#tab_4" data-toggle="tab" aria-expanded="false">Android application Development</a></li>
                  <li class=""><a href="#tab_5" data-toggle="tab" aria-expanded="false">Web Hosting</a></li>
                  <li class=""><a href="#tab_6" data-toggle="tab" aria-expanded="false">SEO and Online Marketing</a></li>
                  <li class="pull-right"><a href="#" class="text-muted"><i class="fa fa-gear"></i></a></li>
                </ul>
                <div class="tab-content">
                  <div class="tab-pane active" id="tab_1">
                    <b>Web Deign and development hires:</b>
    
    
    
    
                    <table class="table" id="webdesignTable">                   
                        <thead>
                            <tr>
                                <th>S.no</th>
                                <th>Order By</th>                                                   
                                <th>Phone</th>
                                <th>Email</th>
                                <th>Message</th>
                                <th>Date</th>                                               
                                <th>Status</th>
                                <th>Options</th>
                            </tr>
                        </thead>
                    </table>
    
    
    
                  </div>
                  <!-- /.tab-pane -->
                  <div class="tab-pane" id="tab_2">
                  <b>Domain Registration</b>
    
    
                  <table class="table" id="domainregistrationTable">                    
                        <thead>
                            <tr>
                                <th>S.no</th>
                                <th>Order By</th>                                                   
                                <th>Phone</th>
                                <th>Email</th>
                                <th>Message</th>
                                <th>Date</th>                                               
                                <th>Status</th>
                                <th>Options</th>
                            </tr>
                        </thead>
                    </table>
    
    
    
                  </div>
                  <!-- /.tab-pane -->
                  <div class="tab-pane" id="tab_3">
                   <b>Cloud Computing</b>
    
    
                  <table class="table" id="cloudcomputingTable">                    
                        <thead>
                            <tr>
                                <th>S.no</th>
                                <th>Order By</th>                                                   
                                <th>Phone</th>
                                <th>Email</th>
                                <th>Message</th>
                                <th>Date</th>                                               
                                <th>Status</th>
                                <th>Options</th>
                            </tr>
                        </thead>
                    </table>
                  </div>
                  <!-- /.tab-pane -->
    
                  <!-- /.tab-pane -->
                  <div class="tab-pane" id="tab_4">
                   <b>Android application Development</b>
    
    
                  <table class="table" id="androidappTable">                    
                        <thead>
                            <tr>
                                <th>S.no</th>
                                <th>Order By</th>                                                   
                                <th>Phone</th>
                                <th>Email</th>
                                <th>Message</th>
                                <th>Date</th>                                               
                                <th>Status</th>
                                <th>Options</th>
                            </tr>
                        </thead>
                    </table>
                  </div>
                  <!-- /.tab-pane -->
    
                  <!-- /.tab-pane -->
                  <div class="tab-pane" id="tab_5">
                   <b>Web Hosting</b>
    
    
                  <table class="table" id="webhostingTable">                    
                        <thead>
                            <tr>
                                <th>S.no</th>
                                <th>Order By</th>                                                   
                                <th>Phone</th>
                                <th>Email</th>
                                <th>Message</th>
                                <th>Date</th>                                               
                                <th>Status</th>
                                <th>Options</th>
                            </tr>
                        </thead>
                    </table>
                  </div>
                  <!-- /.tab-pane -->
    
                  <!-- /.tab-pane -->
                  <div class="tab-pane" id="tab_6">
                   <b>SEO and Online Marketing</b>
    
    
                  <table class="table" id="seoTable">                   
                        <thead>
                            <tr>
                                <th>S.no</th>
                                <th>Order By</th>                                                   
                                <th>Phone</th>
                                <th>Email</th>
                                <th>Message</th>
                                <th>Date</th>                                               
                                <th>Status</th>
                                <th>Options</th>
                            </tr>
                        </thead>
                    </table>
                  </div>
                  <!-- /.tab-pane -->
                </div>
                <!-- /.tab-content -->
                </div>
    
            </div>
    
            <div class="tab-pane fade" id="set2">
                <div class="tabbable">
                       <ul class="nav nav-tabs">
                  <li class="active"><a href="#tab_1" data-toggle="tab" aria-expanded="true">Web Design and Development</a></li>
                  <li class=""><a href="#tab_2" data-toggle="tab" aria-expanded="false">Domain Registration</a></li>
                  <li class=""><a href="#tab_3" data-toggle="tab" aria-expanded="false">Cloud Computing Services</a></li>
                  <li class=""><a href="#tab_4" data-toggle="tab" aria-expanded="false">Android application Development</a></li>
                  <li class=""><a href="#tab_5" data-toggle="tab" aria-expanded="false">Web Hosting</a></li>
                  <li class=""><a href="#tab_6" data-toggle="tab" aria-expanded="false">SEO and Online Marketing</a></li>
                  <li class="pull-right"><a href="#" class="text-muted"><i class="fa fa-gear"></i></a></li>
                </ul>
                <div class="tab-content">
                  <div class="tab-pane active" id="tab_1">
                    <b>Web Deign and development hires:</b>
    
    
    
    
                    <table class="table" id="webdesignTable">                   
                        <thead>
                            <tr>
                                <th>S.no</th>
                                <th>Order By</th>                                                   
                                <th>Phone</th>
                                <th>Email</th>
                                <th>Message</th>
                                <th>Date</th>                                               
                                <th>Status</th>
                                <th>Options</th>
                            </tr>
                        </thead>
                    </table>
    
    
    
                  </div>
                  <!-- /.tab-pane -->
                  <div class="tab-pane" id="tab_2">
                  <b>Domain Registration</b>
    
    
                  <table class="table" id="domainregistrationTable">                    
                        <thead>
                            <tr>
                                <th>S.no</th>
                                <th>Order By</th>                                                   
                                <th>Phone</th>
                                <th>Email</th>
                                <th>Message</th>
                                <th>Date</th>                                               
                                <th>Status</th>
                                <th>Options</th>
                            </tr>
                        </thead>
                    </table>
    
    
    
                  </div>
                  <!-- /.tab-pane -->
                  <div class="tab-pane" id="tab_3">
                   <b>Cloud Computing</b>
    
    
                  <table class="table" id="cloudcomputingTable">                    
                        <thead>
                            <tr>
                                <th>S.no</th>
                                <th>Order By</th>                                                   
                                <th>Phone</th>
                                <th>Email</th>
                                <th>Message</th>
                                <th>Date</th>                                               
                                <th>Status</th>
                                <th>Options</th>
                            </tr>
                        </thead>
                    </table>
                  </div>
                  <!-- /.tab-pane -->
    
                  <!-- /.tab-pane -->
                  <div class="tab-pane" id="tab_4">
                   <b>Android application Development</b>
    
    
                  <table class="table" id="androidappTable">                    
                        <thead>
                            <tr>
                                <th>S.no</th>
                                <th>Order By</th>                                                   
                                <th>Phone</th>
                                <th>Email</th>
                                <th>Message</th>
                                <th>Date</th>                                               
                                <th>Status</th>
                                <th>Options</th>
                            </tr>
                        </thead>
                    </table>
                  </div>
                  <!-- /.tab-pane -->
    
                  <!-- /.tab-pane -->
                  <div class="tab-pane" id="tab_5">
                   <b>Web Hosting</b>
    
    
                  <table class="table" id="webhostingTable">                    
                        <thead>
                            <tr>
                                <th>S.no</th>
                                <th>Order By</th>                                                   
                                <th>Phone</th>
                                <th>Email</th>
                                <th>Message</th>
                                <th>Date</th>                                               
                                <th>Status</th>
                                <th>Options</th>
                            </tr>
                        </thead>
                    </table>
                  </div>
                  <!-- /.tab-pane -->
    
                  <!-- /.tab-pane -->
                  <div class="tab-pane" id="tab_6">
                   <b>SEO and Online Marketing</b>
    
    
                  <table class="table" id="seoTable">                   
                        <thead>
                            <tr>
                                <th>S.no</th>
                                <th>Order By</th>                                                   
                                <th>Phone</th>
                                <th>Email</th>
                                <th>Message</th>
                                <th>Date</th>                                               
                                <th>Status</th>
                                <th>Options</th>
                            </tr>
                        </thead>
                    </table>
                  </div>
                  <!-- /.tab-pane -->
                </div>
                <!-- /.tab-content -->
                </div>
            </div>
    
        </div>
              </div>
            </div>
            </div>
    

    【讨论】:

      【解决方案3】:

      我最近想为我的网站使用 Bootstrap 4 获得嵌套(两级)响应式选项卡。 请在下面找到,我认为这是一个无需额外 JavaScript 即可管理该问题的干净解决方案:

      https://codepen.io/GMaup/pen/eYgJvKd

        
        <div class="card">
          <div class="card-header">
            
            <div class="d-flex">
              <div class="title pt-3 pb-4">
                <h3>Bootstrap 4 - Responsive Nested Tabs without JS (two levels tabs</h3>
              </div>
            </div>
          
            <!-- START TABS DIV -->
            <div class="tabbable-responsive">
              <div class="tabbable">
                <ul class="nav nav-pills nav-justified" id="linxea-avenir" role="tablist">
                  <li class="nav-item">
                    <a class="nav-link active" id="eleve-tab" data-toggle="tab" href="#eleve" role="tab" aria-controls="first" aria-selected="true">Tab 1 with some text</a>
                  </li>
                  <li class="nav-item">
                    <a class="nav-link" id="moyen-tab" data-toggle="tab" href="#moyen" role="tab" aria-controls="second" aria-selected="false">Tab 2 with some text</a>
                  </li>
                  <li class="nav-item">
                    <a class="nav-link" id="faible-tab" data-toggle="tab" href="#faible" role="tab" aria-controls="second" aria-selected="false">Tab 3 with some text</a>
                  </li>
                </ul>
              </div>
            </div>
          
            <div class="tab-content" style="margin-top:2px">
              <div class="tab-pane fade show active" id="eleve" role="tabpanel" aria-labelledby="eleve-tab">
                <!-- START TABS DIV -->
                <div class="tabbable-responsive">
                  <div class="tabbable">
                    <ul class="nav nav-pills nav-justified" id="eleve-nav" role="tablist">
                      <li class="nav-item">
                        <a class="nav-link active" id="eleve-eco-tab" data-toggle="tab" href="#eleve-eco" role="tab" aria-controls="first" aria-selected="true">Tab 1.1 with some text</a>
                      </li>
                      <li class="nav-item">
                        <a class="nav-link" id="eleve-standard-tab" data-toggle="tab" href="#eleve-standard" role="tab" aria-controls="second" aria-selected="false">Tab 1.2 with some text</a>
                      </li>
                    </ul>
                  </div>
                </div>
      
                <div class="tab-content">
                  <div class="tab-pane fade show active" id="eleve-eco" role="tabpanel" aria-labelledby="eleve-eco-tab">
      
      <!-- ****************************************************** -->
      <!-- START SECTION 1 -->
      
      <p>exemple 1</p>
      <p>Resize you screen to see how does it fit</p>
      
      <!-- ****************************************************** -->
      <!-- END SECTION 1 -->
      
                  </div>
                  <div class="tab-pane fade" id="eleve-standard" role="tabpanel" aria-labelledby="eleve-standard-tab">
      
      <!-- ****************************************************** -->
      <!-- START SECTION 2 -->
      
      <p>exemple 2</p>
      
      <!-- ****************************************************** -->
      <!-- END SECTION 2 -->
      
                  </div>
                </div>    
                
              </div>
              <div class="tab-pane fade" id="moyen" role="tabpanel" aria-labelledby="moyen-tab">
                  
              <div class="tab-content" style="margin-top:2px">
              <div class="tab-pane fade show active" id="moyen" role="tabpanel" aria-labelledby="moyen-tab">
                <!-- START TABS DIV -->
                <div class="tabbable-responsive">
                  <div class="tabbable">
                    <ul class="nav nav-pills nav-justified" id="moyen-nav" role="tablist">
                      <li class="nav-item">
                        <a class="nav-link active" id="moyen-eco-tab" data-toggle="tab" href="#moyen-eco" role="tab" aria-controls="first" aria-selected="true">Tab 2.1 with some text</a>
                      </li>
                      <li class="nav-item">
                        <a class="nav-link" id="moyen-standard-tab" data-toggle="tab" href="#moyen-standard" role="tab" aria-controls="second" aria-selected="false">Tab 2.2 with some text</a>
                      </li>
                    </ul>
                  </div>
                </div>
      
                <div class="tab-content">
                  <div class="tab-pane fade show active" id="moyen-eco" role="tabpanel" aria-labelledby="moyen-eco-tab">
      
      <!-- ****************************************************** -->
      <!-- START SECTION 3 -->
      
      <p>exemple 3</p>
      
      <!-- ****************************************************** -->
      <!-- END SECTION 3 -->
      
      
                  </div>
                  <div class="tab-pane fade" id="moyen-standard" role="tabpanel" aria-labelledby="moyen-standard-tab">
      
      <!-- ****************************************************** -->
      <!-- START SECTION 4 -->
      
      <p>exemple 4</p>
      
      <!-- ****************************************************** -->
      <!-- END SECTION 4 -->
                      </div>
                    </div>   
                  </div>
                </div>  
                
              </div>
              
              <div class="tab-pane fade" id="faible" role="tabpanel" aria-labelledby="faible-tab">
                  
              <div class="tab-content" style="margin-top:2px">
              <div class="tab-pane fade show active" id="faible" role="tabpanel" aria-labelledby="faible-tab">
                <!-- START TABS DIV -->
                <div class="tabbable-responsive">
                  <div class="tabbable">
                    <ul class="nav nav-pills nav-justified" id="faible-nav" role="tablist">
                      <li class="nav-item">
                        <a class="nav-link active" id="faible-eco-tab" data-toggle="tab" href="#faible-eco" role="tab" aria-controls="first" aria-selected="true">Tab 3.1 with some text</a>
                      </li>
                      <li class="nav-item">
                        <a class="nav-link" id="faible-standard-tab" data-toggle="tab" href="#faible-standard" role="tab" aria-controls="second" aria-selected="false">Tab 3.2 with some text</a>
                      </li>
                    </ul>
                  </div>
                </div>
      
                <div class="tab-content">
                  <div class="tab-pane fade show active" id="faible-eco" role="tabpanel" aria-labelledby="faible-eco-tab">
              
      <!-- ****************************************************** -->
      <!-- START SECTION 5 -->
      
      <p>exemple 5</p>
      
      <!-- ****************************************************** -->
      <!-- END SECTION 5 -->
      
                  </div>
                  <div class="tab-pane fade" id="faible-standard" role="tabpanel" aria-labelledby="faible-standard-tab">
                      
      <!-- ****************************************************** -->
      <!-- START SECTION 6 -->
      
      <p>exemple 6</p>
      
      <!-- ****************************************************** -->
      <!-- END SECTION 6 -->
              
                      </div>
                    </div>   
                  </div>
                </div>  
                
              </div>
              
            </div>      
            
          </div>
        </div>
      
      
      .nav-pills .nav-link.active, .nav-pills .show>.nav-link {
          color: #fff;
          background-color: #40bf4f !important; 
      }
      
      .tabbable .nav-pills {
        margin-left: 0px;
      }
      

      【讨论】:

      • 这符合我的预期,谢谢朋友:)