问题
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 .
here is my code
<!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>
回答1:
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>
回答2:
use this code in JQUery
$('#changetabbutton').click(function(e){
e.preventDefault();
$('#mytabs a[href="#second"]').tab('show');
})
回答3:
$('#changetabbutton').click(function(e){
e.preventDefault();
var next = $('#mytabs li.active').next()
next.length?
next.find('a').click():
$('#mytabs li a')[0].click();
});
<!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>
回答4:
Try this -
$('#changetabbutton').click(function(e) {
e.preventDefault();
var link = $('#mytabs .active').next().children('a').attr('href');
$('#mytabs a[href="' + link + '"]').tab('show');
});
<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>
回答5:
Saving the tab number as a variable and just adding to it, or resetting it to 1, depending on which tab they're on seems to work. I renamed the id's of the tabs to numbers to fit this solution.
<!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>
来源:https://stackoverflow.com/questions/39451674/how-to-display-next-bootstrap-tab-on-button-click