Twitter Bootstrap Tabs: Go to Specific Tab on Page Reload or Hyperlink

前端 未结 24 1531
夕颜
夕颜 2020-11-22 04:57

I\'m developing a web page in which I\'m using Twitter\'s Bootstrap Framework and their Bootstrap Tabs JS. It works great except for a few minor issues, one of which is I do

24条回答
  •  心在旅途
    2020-11-22 05:06

    This code selects the right tab depending on the #hash and adds the right #hash when a tab is clicked. (this uses jquery)

    In Coffeescript :

    $(document).ready ->
        if location.hash != ''
            $('a[href="'+location.hash+'"]').tab('show')
    
        $('a[data-toggle="tab"]').on 'shown', (e) ->
            location.hash = $(e.target).attr('href').substr(1)
    

    or in JS :

    $(document).ready(function() {
        if (location.hash !== '') $('a[href="' + location.hash + '"]').tab('show');
        return $('a[data-toggle="tab"]').on('shown', function(e) {
          return location.hash = $(e.target).attr('href').substr(1);
        });
    });
    

提交回复
热议问题