简体   繁体   English

下一个和上一个按钮逐步浏览Bootstrap选项卡

[英]Next and Previous buttons step through Bootstrap Tabs

I'm trying to get some Previous and Next buttons to step through some Bootstrap tab panes. 我正在尝试使一些“上一个”和“下一个”按钮逐步通过一些“ Bootstrap”选项卡窗格。

I've read some articles which appear to get close, but not exactly what I am looking for. 我读过一些文章,看起来似乎很接近,但并非完全符合我的期望。

The other examples appear to be looking for the active nav tab, but the next and previous buttons are within a separate tab container and I'm trying to have them independent of them being in a nav container, so that method doesn't really work. 其他示例似乎在寻找active导航选项卡,但是下一个和上一个按钮位于单独的选项卡容器中,而我试图使它们独立于它们位于导航容器中,因此该方法实际上不起作用。

So far I've been using .next() to add the active and show classes to the tab pane, but it seems to add them to all of the tab panes and not just one individually. 到目前为止,我一直在使用.next()active类和show类添加到选项卡窗格中,但似乎将它们添加到所有选项卡窗格中,而不仅仅是将它们单独添加。 So, I'm not 100% if I'm going down the right path, or if there's an easier way to accomplish this? 因此,如果我走对了路,或者有更简单的方法可以做到这一点,我不是100%? It seems like there is and I'm possibly just making it difficult on myself. 好像有,我可能正对自己感到困难。

Anyways, here's what I have so far... 无论如何,这就是我到目前为止所拥有的...

Codepen Link: https://codepen.io/ultraloveninja/pen/OBjmzy/ Codepen链接: https ://codepen.io/ultraloveninja/pen/OBjmzy/

HTML: HTML:

<div class="container">

    <!-- Nav tabs -->
    <ul id="mytabs" class="nav nav-tabs" role="tablist">
      <li class="active">
          <a href="#first" role="tab" data-toggle="tab">
              <icon class="fa fa-home"></icon> First tab
          </a>
      </li>
      <li><a href="#second" role="tab" data-toggle="tab">
          <i class="fa fa-user"></i> Second tab
          </a>
      </li>
      <li>
          <a href="#third" role="tab" data-toggle="tab">
              <i class="fa fa-envelope"></i> Third tab
          </a>
      </li>
    </ul>

    <!-- Tab panes -->
    <div class="tab-content">
      <div class="tab-pane fade active in" id="first">
        <h2>First tab</h2>
      </div>
      <div class="tab-pane fade" id="second">
          <h2>Second tab</h2>
      </div>
      <div class="tab-pane fade" id="third">
          <h2>Third tab</h2>
      </div>
    </div>

  <a class="btn prev" href="#">Previous</a>
  <a class="btn next" href="#">Next</a>

</div>

JS: JS:

$(function(){
  $('#mytabs a:first').tab('show');

  $('.next').on("click",function(e){
    var $this = $(this);
    if($(".tab-pane").hasClass("active")) {
      $('.tab-pane').next().addClass('show active');
      $this.prev().removeClass('show active');
      e.preventDefault();
    }
  });  
});

I figure that if I can figure out the next step through part then I could modify the previous one to follow. 我认为,如果我可以逐步了解下一步,那么可以修改之前的步骤。

Ok after breaking this down about and asking around, I found this: 确定下来并询问周围之后,我发现了:

    $(".next").on("click", function(e) {
    const $active = $(".tab-pane.active");
    const $next = $active.next();

    if ($next.length) {
      $active.removeClass("active show");
      $next.addClass("active show");
    }
    e.preventDefault();
  });

  $(".prev").on("click", function(e) {
    const $active = $(".tab-pane.active");
    const $prev = $active.prev();

    if ($prev.length) {
      $active.removeClass("active show");
      $prev.addClass("active show");
    }
    e.preventDefault();
  });

Biggest thing is that you need to make sure that you have your previous and next buttons outside of the div that holds the tab panes. 最大的事情是,您需要确保在保存选项卡窗格的div 之外具有上一个和下一个按钮。 Otherwise it's going to keep going down the chain. 否则,它将继续下滑。

Here's a very un-refined pen I put together to help illustrate what I am trying to achieve: 这是我整理的一支非常精致的笔,以帮助说明我要实现的目标:

https://codepen.io/ultraloveninja/pen/OBjmzy https://codepen.io/ultraloveninja/pen/OBjmzy

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

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