I'm trying to set a tab active dynamically. I have search the net but the solutions is not working.Here is my code.
<ul id="memnav" class="nav nav-tabs">
<li class="active">
<a class="dropdown-toggle" data-toggle="tab" href="#publication-tab">
Publications
</a>
</li>
<li><a data-toggle="tab" href="#project-tab">Projects</a></li>
<li class="dropdown">
<a class="dropdown-toggle" data-toggle="dropdown" href="#">
About me
<b class="caret"></b>
</a>
<ul class="dropdown-menu">
<li><a data-toggle="tab" href="#education-tab">Education</a></li>
<li><a data-toggle="tab" href="#career-tab">Career</a></li>
<li><a data-toggle="tab" href="#contact-tab">Contact</a></li>
</ul>
</li>
</ul>
and in the script
var active_tab = sessionStorage.getItem('active_tab');
if(active_tab){
$("#memnav").tabs().tabs( "option", "active", active_tab);
}
But it throws this error
Uncaught Error: jQuery UI Tabs: Mismatching fragment identifier.
I have also put the "ul" inside a "div" but it shows the same error
simply use addClass
add .eq()
: https://api.jquery.com/eq/
$(document).ready(function(){ var i=0;//get from storrage $("#memnav li").eq(i).addClass('active'); });
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css"> <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script> <ul id="memnav" class="nav nav-tabs"> <li> <a class="dropdown-toggle" data-toggle="tab" href="#publication-tab"> Publications </a> </li> <li><a data-toggle="tab" href="#project-tab">Projects</a></li> <li class="dropdown"> <a class="dropdown-toggle" data-toggle="dropdown" href="#"> About me <b class="caret"></b> </a> <ul class="dropdown-menu"> <li><a data-toggle="tab" href="#education-tab">Education</a></li> <li><a data-toggle="tab" href="#career-tab">Career</a></li> <li><a data-toggle="tab" href="#contact-tab">Contact</a></li> </ul> </li> </ul>
You can try this as well :)
$(document).ready(function(){ var active_tab = '#publication-tab'; $('#memnav').find('a[href="'+active_tab+'"]').parent().addClass('active'); });
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css"> <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script> <ul id="memnav" class="nav nav-tabs"> <li> <a class="dropdown-toggle" data-toggle="tab" href="#publication-tab"> Publications </a> </li> <li><a data-toggle="tab" href="#project-tab">Projects</a></li> <li class="dropdown"> <a class="dropdown-toggle" data-toggle="dropdown" href="#"> About me <b class="caret"></b> </a> <ul class="dropdown-menu"> <li><a data-toggle="tab" href="#education-tab">Education</a></li> <li><a data-toggle="tab" href="#career-tab">Career</a></li> <li><a data-toggle="tab" href="#contact-tab">Contact</a></li> </ul> </li> </ul>
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.