Bootstrap 3 jquery event for active tab change

前端 未结 6 1456
挽巷
挽巷 2020-11-28 17:22

I spent an unrealistic amount of time trying to fire a function when the tab changes of the bootstrap 3 tab/navbar and literally all suggestions google spat out wer

6条回答
  •  野性不改
    2020-11-28 18:22

    To add to Mosh Feu answer, if the tabs where created on the fly like in my case, you would use the following code

    $(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');
        }
    
    });
    

    I hope this helps someone

提交回复
热议问题