jQuery 通过 JS 获取当前活动标签。

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/19751109/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-27 00:32:55  来源:igfitidea点击:

Get Current Active Tab through JS.

jqueryhtmltwitter-bootstrap

提问by newbie

I want to display the current tab as active in BOOTSTRAP.

我想在 BOOTSTRAP 中将当前选项卡显示为活动的。

<ul class="nav nav-tabs">               
    <!-- Default tab -->
    <li class="active"><a href="#elemLower_tab" data-toggle="tab">Elementary Lower</a></li>
    <li><a href="#elemAdvance_tab" data-toggle="tab">Elementary Advance</a></li>
    <li><a href="#secondary_tab" data-toggle="tab">Secondary</a></li>               
</ul>

I want to get the current tab active through jQuery? Is there a way to do this?

我想通过 jQuery 激活当前选项卡吗?有没有办法做到这一点?

回答by Arun P Johny

Try

尝试

$('.nav-tabs .active').text()

回答by Dhanith Suryavansi

Use this JS Code.

使用此 JS 代码。

$(function(){
var url = window.location.pathname,
urlRegExp = new RegExp(url.replace(/\/$/,'') + "$");
$('ul.nav-tabs li a').each(function(){
if(urlRegExp.test(this.href.replace(/\/$/,''))){
$(this).addClass('active');
}
});
});

Use CSS:Attach Active class to

使用 CSS:Attach Active 类

ul.nav-tabs a.active{color:#000;}

回答by Nick McCurdy

If you want to run JavaScript code whenever the current tab changes, see showand shownevents in the tabs plugin documentation.

如果你想运行JavaScript代码,只要在当前选项卡的变化,看到showshown事件中的标签插件文档