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

前端 未结 24 1452
夕颜
夕颜 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:02

    Here is what i did, really simple, and provided your tab links have an ID associated with them you can get the href attribute and pass that over to the function that shows the tab contents:

    <script type="text/javascript">
            jQuery(document).ready(function() {
                var hash = document.location.hash;
                var prefix = "tab_";
                if (hash) {
                    var tab = jQuery(hash.replace(prefix,"")).attr('href');
                    jQuery('.nav-tabs a[href='+tab+']').tab('show');
                }
            });
            </script>
    

    Then in your url you can add the hash as something like: #tab_tab1, the 'tab_' part is removed from the hash itself so the ID of the actual tab link in the nav-tabs (tabid1) is placed after this, so your url would look something like: www.mydomain.com/index.php#tab_tabid1.

    This works perfect for me and hope it helps someone else :-)

    0 讨论(0)
  • 2020-11-22 05:02

    This is my solution to handle nested tabs. I just added a function to check if the active tab has a parent tab to be activated. This is the function:

    function activateParentTab(tab) {
        $('.tab-pane').each(function() {
            var cur_tab = $(this);
            if ( $(this).find('#' + tab).length > 0 ) {
                $('.nav-tabs a[href=#'+ cur_tab.attr('id') +']').tab('show');
                return false;
            }
        });
    }
    

    And can be called like this (Based on @flynfish's solution):

    var hash = document.location.hash;
    var prefix = "";
    if (hash) {
        $('.nav-tabs a[href='+hash.replace(prefix,"")+']').tab('show');
        activateParentTab(hash);
    } 
    
    // Change hash for page-reload
    $('.nav-tabs a').on('shown', function (e) {
        window.location.hash = e.target.hash.replace("#", "#" + prefix);
    });
    

    This solution works pretty fine to me at the moment. Hope this can be useful for someone else ;)

    0 讨论(0)
  • 2020-11-22 05:02

    I just had this issue, but needed to handle multiple tab levels. The code is rather ugly (see comments), but does its job: https://gist.github.com/JensRantil/4721860 Hopefully someone else will find it useful (and feel free to propose better solutions!).

    0 讨论(0)
  • 2020-11-22 05:04

    Here is my solution to the problem, a bit late perhaps. But it could maybe help others:

    // Javascript to enable link to tab
    var url = document.location.toString();
    if (url.match('#')) {
        $('.nav-tabs a[href="#' + url.split('#')[1] + '"]').tab('show');
    } 
    
    // Change hash for page-reload
    $('.nav-tabs a').on('shown.bs.tab', function (e) {
        window.location.hash = e.target.hash;
    })
    
    0 讨论(0)
  • 2020-11-22 05:04

    This works in Bootstrap 3 and improves dubbe and flynfish 's 2 top answers by integrating GarciaWebDev 's answer as well (which allows for url parameters after the hash and is straight from Bootstrap authors on the github issue tracker):

    // Javascript to enable link to tab
    var hash = document.location.hash;
    var prefix = "tab_";
    
    if (hash) {
        hash = hash.replace(prefix,'');
        var hashPieces = hash.split('?');
        activeTab = $('.nav-tabs a[href=' + hashPieces[0] + ']');
        activeTab && activeTab.tab('show');
    } 
    
    // Change hash for page-reload
    $('.nav-tabs a').on('shown', function (e) {
        window.location.hash = e.target.hash.replace("#", "#" + prefix);
    });
    
    0 讨论(0)
  • 2020-11-22 05:05

    I came up with a solution that uses the url hash or localStorage depending on the availability of the latter with below code:

    $(function(){
        $(document).on('shown.bs.tab', 'a[data-toggle="tab"]', function (e) {
            localStorage.setItem('activeTab', $(e.target).attr('href'));
        })
    
        var hash = window.location.hash;
        var activeTab = localStorage.getItem('activeTab');
    
        if(hash){
              $('#project-tabs  a[href="' + hash + '"]').tab('show');   
        }else if (activeTab){
            $('#project-tabs a[href="' + activeTab + '"]').tab('show');
        }
    });
    
    0 讨论(0)
提交回复
热议问题