javascript - Bootstrap: next button should display next tab - Stack Overflow

I want to implement a custom next button into the bootstrap tab navigation.When clicking on the next-bu

I want to implement a custom next button into the bootstrap tab navigation.

When clicking on the next-button, the next tab should be displayed as active. If the last tab is reached it should start with the first tab.

Bootply snippet

I want to implement a custom next button into the bootstrap tab navigation.

When clicking on the next-button, the next tab should be displayed as active. If the last tab is reached it should start with the first tab.

Bootply snippet

Share Improve this question asked Nov 7, 2015 at 4:40 herrhherrh 1,3681 gold badge17 silver badges34 bronze badges
Add a ment  | 

3 Answers 3

Reset to default 4

This solve your problem

$('.next-tab').click(function(e){
  if($('.nav-tabs > .active').next('li').hasClass('next-tab')){
    $('.nav-tabs > li').first('li').find('a').trigger('click');
  }else{
    $('.nav-tabs > .active').next('li').find('a').trigger('click');
  }
  e.preventDefault();
});

http://www.bootply./XYpxMIgink

I Recently needed this functionality and this is what I ended up with:

$('.change-tab').click(function() {
  var $this = $(this);
  var $nav = $($this.data('target'))
  var $tabs = $nav.find('li');
  var $curTab = $tabs.filter('.active');
  var cur = $tabs.index($curTab);
  if ($this.data('direction') == 'next') var $showTab = cur == $tabs.length - 1 ? $tabs.eq(0).find('a') : $tabs.eq(cur + 1).find('a');
  else var $showTab = cur == 0 ? $tabs.eq($tabs.length - 1).find('a') : $tabs.eq(cur - 1).find('a');
  $showTab.tab('show');
});



// normal tabs code
$('#myTab a').click(function (e) {
	 e.preventDefault();
	 $(this).tab('show');
});

$(function () {
$('#myTab a:last').tab('show');
})
<link href="https://netdna.bootstrapcdn./bootstrap/3.0.0/css/bootstrap.min.css" rel="stylesheet" />
<script src="https://ajax.googleapis./ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script src="https://netdna.bootstrapcdn./bootstrap/3.0.0/js/bootstrap.min.js"></script>
<div class="container">
  <ul class="nav nav-tabs" id="myTab">
    <li class="active"><a href="#home">Home</a>
    </li>
    <li><a href="#profile">Profile</a>
    </li>
    <li><a href="#messages">Messages</a>
    </li>
    <li><a href="#settings">Settings</a>
    </li>
  </ul>

  <div class="tab-content">
    <div class="tab-pane active" id="home">Home content...</div>
    <div class="tab-pane" id="profile">Content here...</div>
    <div class="tab-pane" id="messages">Messages...</div>
    <div class="tab-pane" id="settings">Settings...</div>
  </div>
</div>


<div class="row">
  <div class="col-md-12">
    <div class="btn-toolbar pull-right">
      <div class="btn-group">
        <button class="btn btn-default change-tab" data-direction="previous" data-target="#myTab"><span class="glyphicon glyphicon-arrow-left" aria-hidden="true"></span> Last</button>
        <button class="btn btn-default change-tab" data-direction="next" data-target="#myTab">Next <span class="glyphicon glyphicon-arrow-right" aria-hidden="true"></span>
        </button>
      </div>
    </div>
  </div>
</div>

I am using @DelightedD0D provided solution. When the page load, the control stop on the last tab always. If you want to fix this, please update the following function

$(function () {
$('#myTab a:last').tab('show');
})

to

$(function () {
$('#myTab a:active').tab('show');
})

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745076097a4609844.html

相关推荐

  • javascript - Bootstrap: next button should display next tab - Stack Overflow

    I want to implement a custom next button into the bootstrap tab navigation.When clicking on the next-bu

    18小时前
    30

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信