简体   繁体   English

显示活动内容时,Bootstrap 3 选项卡切换为活动状态

[英]Bootstrap 3 tab toggle active when active content is shown

I have some tabs using bootstrap 3. The way it is setup is there is 2 tab navs that display the same content.我有一些使用引导程序 3 的选项卡。它的设置方式是有 2 个选项卡导航显示相同的内容。 What I can't seem to figure out is if say Tab A is active then both Tab A list triggers should have the class Active.我似乎无法弄清楚的是,如果说 Tab A 处于活动状态,那么两个 Tab A 列表触发器都应该使 class 处于活动状态。 Right now it is only on the li trigger that you click.现在它只在您单击的 li 触发器上。

codepen: https://codepen.io/mDDDD/pen/qBqbRXJ代码笔: https://codepen.io/mDDDD/pen/qBqbRXJ

The way the codepen is setup is not exactly how my project is setup but gives an idea. codepen 的设置方式并不完全是我的项目的设置方式,而是给出了一个想法。 Currently when you land on my page there is a left tab nav and in the middle is another tab nav section and when you click the center section is hidden and the tab content fades in:目前,当您登陆我的页面时,有一个左侧选项卡导航,中间是另一个选项卡导航部分,当您单击中心部分时,选项卡内容将隐藏:

$('.tab-toggle').on('click', function () {
      $('.center-nav-blocks').fadeOut('fast', function () {
        $('.tab-content').fadeIn('fast');

        scrollToElement('.title-row');
      });
    });

You can use index() method to get the index of li which is clicked then using this index add class to li where index matches.您可以使用index()方法获取单击的li的索引,然后使用此索引将 class 添加到索引匹配的li中。

Demo Code :演示代码

 $('.tab-toggle').on('click', function() { //get index of li which is clicked var indexs = $(this).closest('li').index() //remove class from others $("ul li").not($(this).closest('li')).removeClass("active") //add class where indexs same $("ul").find("li:eq(" + indexs + ")").not($(this).closest('li')).addClass("active") });
 <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.5/js/bootstrap.min.js"></script> <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.5/css/bootstrap.min.css"> <ul class="nav-pills nav-stacked"> <li><a class="tab-toggle" href="#tab-a" data-value="#a" data-toggle="tab">Tab A</a></li> <li><a class="tab-toggle" href="#tab-b" data-value="#b" data-toggle="tab">Tab B</a></li> <li><a class="tab-toggle" href="#tab-c" data-value="#c" data-toggle="tab">Tab C</a></li> <li><a class="tab-toggle" href="#tab-d" data-value="#d" data-toggle="tab">Tab D</a></li> </ul> <div class="tab-content"> <div class="tab-pane" data-id="a" id="tab-a">A content</div> <div class="tab-pane" data-id="b" id="tab-b">B Content</div> <div class="tab-pane" data-id="c" id="tab-c"> C Content </div> <div class="tab-pane" data-id="d" id="tab-d">D content </div> </div> <ul class="nav-pills nav-stacked"> <li><a class="tab-toggle" href="#tab-a" data-value="#a" data-toggle="tab">Tab A</a></li> <li><a class="tab-toggle" href="#tab-b" data-value="#b" data-toggle="tab">Tab B</a></li> <li><a class="tab-toggle" href="#tab-c" data-value="#c" data-toggle="tab">Tab C</a></li> <li><a class="tab-toggle" href="#tab-d" data-value="#d" data-toggle="tab">Tab D</a></li> </ul>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM