使用 jquery 动态设置活动标签

Dynamically set active tab with jquery

我正在尝试动态设置标签页。我已经在网上搜索但解决方案不是 working.Here 是我的代码。

<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>

并在脚本中

var active_tab = sessionStorage.getItem('active_tab');
        if(active_tab){
            $("#memnav").tabs().tabs( "option", "active", active_tab);
        }

但是它抛出这个错误

Uncaught Error: jQuery UI Tabs: Mismatching fragment identifier.

我也将 "ul" 放在 "div" 中,但它显示相同的错误

只需使用 addClass
添加 .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>

你也可以试试这个:)

$(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>