如何在按钮单击上显示下一个引导程序选项卡 [英] how to display next bootstrap tab on button click
问题描述
我要在单击按钮时一个接一个地显示下一个引导程序选项卡。现在在单击按钮时,仅显示第二个选项卡,因为我已经对其进行了硬编码,应该如何代替href =#second的显示方式当我第一次单击其他选项卡而不是单击活动的第一个选项卡时,用户必须收到错误消息:用户必须先单击第一个选项卡,然后单击其他选项卡插件意味着禁用除页面加载的第一个选项卡之外的其他选项卡。
I want to display next bootstrap tabs one by one on button click.Right now on button click only the second tab is displayed as i have hard coded it what should be in place of the href="#second" how to get the particular id in the href?When I first click on other tabs instead of clicking on the first tab which is active then the user must get the error message that ' the user have to click the first tab and then the other tabs' using noty plugin means disable the other tabs except for the first tab on page load .
这是我的代码
<!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>
推荐答案
通过使用 $(' .nav-tabs> .active')。next('li')。find('a')
选择器,您可以选择下一个标签并将其激活。如果找不到下一个选择器,则使第一个选项卡处于活动状态。
By using $('.nav-tabs > .active').next('li').find('a')
selector you can select next tab and make it active. And if next selector not found make the first tab active.
$(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>
这篇关于如何在按钮单击上显示下一个引导程序选项卡的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!