$("a[href='#tab_target_id']").on('shown.bs.tab', function(e) {
console.log('shown - after the tab has been shown');
});
// or even this one if we want the earlier event
$("a[href='#tab_target_id']").on('show.bs.tab', function(e) {
console.log('show - before the new tab has been shown');
});
$(document).on('shown.bs.tab', 'a[data-toggle="tab"]', function (e) {
var tab = $(e.target);
var contentId = tab.attr("href");
//This check if the tab is active
if (tab.parent().hasClass('active')) {
console.log('the tab with the content id ' + contentId + ' is visible');
} else {
console.log('the tab with the content id ' + contentId + ' is NOT visible');
}
});
6 回答
Eric Saupe knows how to do it
问题是attr('href')永远不会是空的 .
或者比较#id =“#some value”然后调用ajax .
感谢@ Gerben的帖子后来了解有两个事件show.bs.tab(显示选项卡之前)和shown.bs.tab(显示选项卡之后),如文档中所述 - Bootstrap Tab usage
如果我们只对特定选项卡感兴趣,并且可能添加单独的函数而不必在一个函数中添加if - else块,那么另一个解决方案是使用a href选择器(如果需要,可以使用其他选择器)
demo
要添加到Mosh Feu的答案,如果像我一样在运行中创建的选项卡,您将使用以下代码
我希望这可以帮助别人
这对我有用 .
我用另一种方法 .
只是尝试找到所有
a
id
从某些 substring 开始 .JS
HTML