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

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

    If it matters to anybody, the following code is small and works flawless, to get a single hash value from the URL and show that:

    <script>
        window.onload = function () {
            let url = document.location.toString();
            let splitHash = url.split("#");
            document.getElementById(splitHash[1]).click();
        };
    </script>
    

    what it does is it retrieves the id and fires the click event. Simple.

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

    While the JavaScript solution provided may work, I went a slightly different way that requires no additional JavaScript, but does require logic in your view. You create a link with a standard URL parameter, like:

    <a href = "http://link.to.yourpage?activeTab=home">My Link</a>
    

    Then you simply detect the value of activeTab to write 'class="active"' in the appropriate <li>

    Pseudocode (implement accordingly in your language). Note I've set 'home' tab as a default active if no parameter provided in this example.

    $activetabhome = (params.activeTab is null or params.activeTab == 'home') ? 'class="active"' : '';
    $activetabprofile = (params.activeTab == 'profile') ? 'class="active"' : '';
    
    <li $activetabhome><a href="#home">Home</a></li>
    <li $activetabprofile><a href="#profile">Profile</a></li>
    
    0 讨论(0)
  • 2020-11-22 05:23

    For Bootstrap 3:

    $('.nav-tabs a[href="#' + tabID + '"]').tab('show');
    

    https://jsfiddle.net/DTcHh/6638/

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

    I make sth like this for links with ajax #!# (e.g./test.com#!#test3) but you can modify it whatever you like

    $(document).ready(function() {
    
           let hash = document.location.hash;
           let prefix = "!#";
    
           //change hash url on page reload
           if (hash) {
             $('.nav-tabs a[href=\"'+hash.replace(prefix,"")+'\"]').tab('show');
           } 
    
           // change hash url on switch tab
           $('.nav-tabs a').on('shown.bs.tab', function (e) {
              window.location.hash = e.target.hash.replace("#", "#" + prefix);
           });
     });
    

    Example with simple page on Github here

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

    Building on Demircan Celebi's solution; I wanted the tab to open when modifying the url and open tab without having to reload the page from the server.

    <script type="text/javascript">
        $(function() {
            openTabHash(); // for the initial page load
            window.addEventListener("hashchange", openTabHash, false); // for later changes to url
        });
    
    
        function openTabHash()
        {
            console.log('openTabHash');
            // Javascript to enable link to tab
            var url = document.location.toString();
            if (url.match('#')) {
                $('.nav-tabs a[href="#'+url.split('#')[1]+'"]').tab('show') ;
            } 
    
            // With HTML5 history API, we can easily prevent scrolling!
            $('.nav-tabs a').on('shown.bs.tab', function (e) {
                if(history.pushState) {
                    history.pushState(null, null, e.target.hash); 
                } else {
                    window.location.hash = e.target.hash; //Polyfill for old browsers
                }
            })
        }
    </script>
    
    0 讨论(0)
  • 2020-11-22 05:27

    you could trigger a click event on the corresponding tab link:

    $(document).ready(function(){
    
      if(window.location.hash != "") {
          $('a[href="' + window.location.hash + '"]').click()
      }
    
    });
    
    0 讨论(0)
提交回复
热议问题