Capturing 'shown' event from bootstrap tab

前端 未结 5 2058
再見小時候
再見小時候 2021-02-05 03:03

I have some \'static\' HTML on my page:

    @*
  • nodes will b
5条回答
  •  半阙折子戏
    2021-02-05 03:39

    The correct event binding for tab change is shown.bs.tab.

    $(document).on('shown.bs.tab', 'a[data-toggle="tab"]', function (e) {
        alert('TAB CHANGED');
    })
    

    Update 11-01-2020 --- Bootstrap 4.5

    This is still the correct answer however, this is a bit of additional helpful information found all the way at the bottom of the official bootstrap docs page at: https://getbootstrap.com/docs/4.5/components/navs/#tabs

    $('a[data-toggle="tab"]').on('shown.bs.tab', function (e) {
      e.target // newly activated tab
      e.relatedTarget // previous active tab
    })
    

    You can determine which tab has been selected each time the code fires with e.target.

    If you have unique IDs on your elements then you could do something like the following so code only runs when the appropriate tab is clicked.

    $('a[data-toggle="tab"]').on('shown.bs.tab', function (e) {
      switch (e.target.id){
          case "mainTab":{
               doMainTabStuff();
               break;
          }
          case "configTab":{
               doConfigTabStuff();
               break;
          }
      }
    })
    

提交回复
热议问题