Bootstrap 3 jquery event for active tab change

前端 未结 6 1452
挽巷
挽巷 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:05

    Thanks to @Gerben's post came to know there are two events show.bs.tab (before the tab is shown) and shown.bs.tab (after the tab is shown) as explained in the docs - Bootstrap Tab usage

    An additional solution if we're only interested in a specific tab, and maybe add separate functions without having to add an if - else block in one function, is to use the a href selector (maybe along with additional selectors if required)

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

    demo

提交回复
热议问题