I am new to Bootstrap, and can't seem to figure this out, i have navbar being used as javascript tab functionality of Bootstrap, which can have dynamically added and removeable nav-links, i have used images rather than default nav-links, my question is when i add the dynamic nav-link, it should become the active class and show its relevant content, at the moment i have to click to make it active, and if i remove any nav-link, the content remains same, is there a way to achieve this function
The html is:
<ul id="nav-tabs" data-tabs="tabs" >
<li class="test" ><img src="assets/img/button_home_selected3.png" class="hover" width="83" /><span>Home</span></a> </li>
</ul>
The tabs are added when this button is clicked:
<a href="#" class="plus" title="Click to add Tabs" ><img src="assets/img/icon_plus.png"/></a>
The li are added using
var counter = 1;
$('.plus').click(function(e) {
e.preventDefault();
var li_count = $('#nav-tabs').find("li.test").length;
if (li_count <= 3)
if(counter <= 3){
$('#nav-tabs').append('<li class="test" ><img src="assets/img/button_home_selected3.png" class="hover" width="83" /><span>Home</span></a><button type="button" class="close">×</button></div></a></li>');
} else { alert("Only 3 Tabs Allowed!")};
The content of tabs are added similarly later;
The active class in tabs is toggled using
$("#nav-tabs").on("click", "a", function(e) {
e.preventDefault();
$(this).tab('show');
$('li.test').each(function() {
if($(this).hasClass('active'))
{
//Active class is applied
$(this).children().children().closest("img").attr("src", "assets/img/button_home_selected3.png");
$(this).find("button").show();
}
else
{
$(this).children().children().closest("img").attr("src", "assets/img/button_home_plain2.png");
$(this).find("button").hide();
}
});
The li are closed using button close in the new nav-links as:
$('.close').click(function(e) {
e.preventDefault();
var panelId = $(this).closest("li").remove().attr("aria-controls");
$("#tab" + panelId).remove();
$("#nav-tabs").children("li").last().addClass("active");
if(counter <= 1){
counter = 1;
}else if (counter > 1) {
counter--;
}
return false;
})
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…