如何在按钮单击时显示下一个引导选项卡

时间:2016-09-12 13:37:47

标签: jquery bootstrap-tabs

我想在按钮点击时逐个显示下一个引导标签。现在按钮点击只显示第二个标签,因为我已经硬编码它应该代替href =“#second”如何获得href中的特定id?当我第一次单击其他选项卡而不是单击第一个处于活动状态的选项卡时,用户必须收到错误消息“用户必须单击第一个选项卡,然后使用noty单击其他选项卡”插件意味着禁用除页面加载时第一个选项卡之外的其他选项卡。

这是我的代码

<!DOCTYPE html>
<html lang="en">
<head>
  <title>Bootstrap Case</title>
  <meta charset="utf-8">
  <meta name="viewport" content="width=device-width, initial-scale=1">
  <link rel="stylesheet" href="http://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css">
  <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.4/jquery.min.js"></script>
  <script src="http://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>
  <script>
  $(function(){

        $('#changetabbutton').click(function(e){
            e.preventDefault();

            $('#mytabs a[href="#second"]').tab('show');
        });

    });
  </script>
</head>
<body>

<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">
              <i class="fa fa-home"></i> 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>
      <li>
          <a href="#fourth" role="tab" data-toggle="tab">
              <i class="fa fa-envelope"></i> Fourth tab
          </a>
      </li>
      <li>
          <a href="#fourth" role="tab" data-toggle="tab">
              <i class="fa fa-envelope"></i> Fifth tab
          </a>
      </li>
    </ul>

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

    </div>
     <button type="button" id="changetabbutton" class="btn btn-primary ">Set next tab</button>
</div>

</body>
</html>

5 个答案:

答案 0 :(得分:3)

使用$('.nav-tabs > .active').next('li').find('a')选择器,您可以选择下一个标签并将其激活。如果找不到下一个选择器,则第一个选项卡处于活动状态。

$(function(){

  $('#changetabbutton').click(function(e){
    e.preventDefault();
    var next_tab = $('.nav-tabs > .active').next('li').find('a');
    if(next_tab.length>0){
      next_tab.trigger('click');
    }else{
      $('.nav-tabs li:eq(0) a').trigger('click');
    }
  });
});
<link rel="stylesheet" href="http://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css">
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.4/jquery.min.js"></script>
<script src="http://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>
<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">
        <i class="fa fa-home"></i> 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>
    <li>
      <a href="#fourth" role="tab" data-toggle="tab">
        <i class="fa fa-envelope"></i> Fourth tab
      </a>
    </li>
    <li>
      <a href="#fourth" role="tab" data-toggle="tab">
        <i class="fa fa-envelope"></i> Fifth tab
      </a>
    </li>
  </ul>

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

  </div>
  <button type="button" id="changetabbutton" class="btn btn-primary ">Set next tab</button>
</div>

答案 1 :(得分:0)

在JQUery中使用此代码

$('#changetabbutton').click(function(e){
        e.preventDefault();
        $('#mytabs a[href="#second"]').tab('show');
    })

答案 2 :(得分:0)

    $('#changetabbutton').click(function(e){
        e.preventDefault();
        var next = $('#mytabs li.active').next()
        next.length?
          next.find('a').click():
          $('#mytabs li a')[0].click();
    });

&#13;
&#13;
<!DOCTYPE html>
<html lang="en">
<head>
  <title>Bootstrap Case</title>
  <meta charset="utf-8">
  <meta name="viewport" content="width=device-width, initial-scale=1">
  <link rel="stylesheet" href="http://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css">
  <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.4/jquery.min.js"></script>
  <script src="http://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>
  <script>
  $(function(){

        $('#changetabbutton').click(function(e){
            e.preventDefault();
            var next = $('#mytabs li.active').next()
            next.length?
              next.find('a').click():
              $('#mytabs li a')[0].click();
        });

    });
  </script>
</head>
<body>

<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">
              <i class="fa fa-home"></i> 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>
      <li>
          <a href="#fourth" role="tab" data-toggle="tab">
              <i class="fa fa-envelope"></i> Fourth tab
          </a>
      </li>
      <li>
          <a href="#fourth" role="tab" data-toggle="tab">
              <i class="fa fa-envelope"></i> Fifth tab
          </a>
      </li>
    </ul>

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

    </div>
     <button type="button" id="changetabbutton" class="btn btn-primary ">Set next tab</button>
</div>
&#13;
&#13;
&#13;

答案 3 :(得分:0)

试试这个 -

&#13;
&#13;
$('#changetabbutton').click(function(e) {
  e.preventDefault();
  var link = $('#mytabs .active').next().children('a').attr('href');

  $('#mytabs a[href="' + link + '"]').tab('show');
});
&#13;
<link rel="stylesheet" href="http://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css">
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script src="http://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>
<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">
        <i class="fa fa-home"></i> 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>
    <li>
      <a href="#fourth" role="tab" data-toggle="tab">
        <i class="fa fa-envelope"></i> Fourth tab
      </a>
    </li>
    <li>
      <a href="#fifth" role="tab" data-toggle="tab">
        <i class="fa fa-envelope"></i> Fifth tab
      </a>
    </li>
  </ul>

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

  </div>
  <button type="button" id="changetabbutton" class="btn btn-primary ">Set next tab</button>
</div>
&#13;
&#13;
&#13;

答案 4 :(得分:0)

将标签号保存为变量,只需将其添加到其中,或将其重置为1,具体取决于它们所在的选项卡似乎有效。我将标签的id重命名为数字以适应此解决方案。

<!DOCTYPE html>
<html lang="en">
<head>
  <title>Bootstrap Case</title>
  <meta charset="utf-8">
  <meta name="viewport" content="width=device-width, initial-scale=1">
  <link rel="stylesheet" href="http://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css">
  <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.4/jquery.min.js"></script>
  <script src="http://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>
  <script>
  $(function(){
		
		var tab = 1;
		
        $('#changetabbutton').click(function(e){
            e.preventDefault();
			if(tab>4){
				tab = 1;
			}else{
				tab++;
			}
            $('#mytabs a[href="#'+tab+'"]').tab('show');
        });

    });
  </script>
</head>
<body>

<div class="container">

    <!-- Nav tabs -->
    <ul id="mytabs" class="nav nav-tabs" role="tablist">
      <li class="active">
          <a href="#1" role="tab" data-toggle="tab">
              <i class="fa fa-home"></i> First tab
          </a>
      </li>
      <li><a href="#2" role="tab" data-toggle="tab">
          <i class="fa fa-user"></i> Second tab
          </a>
      </li>
      <li>
          <a href="#3" role="tab" data-toggle="tab">
              <i class="fa fa-envelope"></i> Third tab
          </a>
      </li>
      <li>
          <a href="#4" role="tab" data-toggle="tab">
              <i class="fa fa-envelope"></i> Fourth tab
          </a>
      </li>
      <li>
          <a href="#5" role="tab" data-toggle="tab">
              <i class="fa fa-envelope"></i> Fifth tab
          </a>
      </li>
    </ul>

    <!-- Tab panes -->
    <div class="tab-content">
      <div class="tab-pane fade active in cont" id="1">
          <h2>First tab</h2>
      </div>
      <div class="tab-pane fade cont" id="2">
          <h2>Second tab</h2>
      </div>
      <div class="tab-pane fade cont" id="3">
          <h2>Third tab</h2>
      </div>
      <div class="tab-pane fade cont" id="4">
          <h2>Fourth tab</h2>
      </div>
       <div class="tab-pane fade cont" id="5">
          <h2>Fifth tab</h2>
      </div>

    </div>
     <button type="button" id="changetabbutton" class="btn btn-primary ">Set next tab</button>
</div>

</body>
</html>