Twitter Bootstrap - Tabs - URL doesn't change

前端 未结 15 1539
长情又很酷
长情又很酷 2020-11-28 18:53

I\'m using Twitter Bootstrap and its \"tabs\".

I have the following code:

相关标签:
15条回答
  • 2020-11-28 19:15

    Using data-toggle="tab" asks the plugin to handle the tabs, which while doing it calls preventDefault on the event (code on github).

    You can use your own tab activation, and let the event go through :

    $('.nav-tabs a').click(function (e) {
        // No e.preventDefault() here
        $(this).tab('show');
    });
    

    And you must remove the attribute data-toggle="tab"

    Check the doc if you have doubts.

    0 讨论(0)
  • 2020-11-28 19:16

    My solution to your problem:

    First add new data-value attribute to each a link, like this:

    <ul class="nav nav-tabs">
        <li class="active">
            <a data-toggle="tab" href="#tab-add" data-value="#add">add</a>
        </li>
        <li>
            <a data-toggle="tab" href="#tab-edit" data-value="#edit">edit</a>
        </li>
        <li>
            <a data-toggle="tab" href="#tab-delete" data-value="#delete">delete</a>
        </li>
    </ul>
    

    Second, change ids of tabs, e.g. from add to tab-add , also update hrefs to include tab- prefix:

    <div class="tab-content">
        <div class="tab-pane" id="tab-add">Add panel</div>
        <div class="tab-pane" id="tab-edit">Edit panel</div>
        <div class="tab-pane" id="tab-delete">Panel panel</div>
    </div>
    

    And finally js code:

    <script type="text/javascript">
        $(function () {
            var navTabs = $('.nav-tabs a');
            var hash = window.location.hash;
            hash && navTabs.filter('[data-value="' + hash + '"]').tab('show');
    
            navTabs.on('shown', function (e) {
                var newhash = $(e.target).attr('data-value');
                window.location.hash = newhash;
            });
        })
    </script>
    

    Here is jsFiddle - but it does not work because of iframe used by jsFiddle, but you can read source of my solution.

    0 讨论(0)
  • 2020-11-28 19:16

    I understood that OP said using JQuery but you can simply use vanilla JS to do that:

    document.querySelectorAll('ul.nav a.nav-link').forEach(link => {
        link.onclick = () => window.history.pushState(null, null, link.attributes.href.value);
    });
    
    0 讨论(0)
  • 2020-11-28 19:21

    Thanks to @tomaszbak for the original solution however since my edits got rejected and I'm unable to comment on his post yet I will leave my slightly improved and more readable version here.

    It does basically the same thing but if fixes the cross browser compatibility issue I had with his.

    $(document).ready(function(){
       // go to hash on window load
       var hash = window.location.hash;
       if (hash) {
           $('ul.nav a[href="' + hash + '"]').tab('show');
       }
       // change hash on click
       $('.nav-tabs a').click(function (e) {
           $(this).tab('show');
           if($('html').scrollTop()){
               var scrollmem = $('html').scrollTop(); // get scrollbar position (firefox)
           }else{
               var scrollmem = $('body').scrollTop(); // get scrollbar position (chrome)
           }
           window.location.hash = this.hash;
           $('html,body').scrollTop(scrollmem); // set scrollbar position
       });
    });
    
    0 讨论(0)
  • 2020-11-28 19:21

    None of the above worked for me, so here's my how I got mine working:

    1. Add class="tab-link" to all links needing this functionality
    2. Add the following code to your javascript:
        window.addEventListener
        (
            'popstate',
            function( event )
            {
                tab_change();
            }
        );
    
        function tab_change()
        {
            var hash = window.location.hash;
            hash && $( 'ul.nav a[href="' + hash + '"]' ).tab( 'show' );
    
            $( '.tab-link' ).click
            (
                function( e )
                {
                    $( this ).tab( 'show' );
    
                    var scrollmem = $( 'body' ).scrollTop() || $( 'html' ).scrollTop();
                    window.location.hash = this.hash;
                    $( 'html,body' ).scrollTop( scrollmem );
    
                    $( 'ul.nav-tabs a[href="' + window.location.hash + '"]' ).tab( 'show' );
                }
            );
        }
    
    
    0 讨论(0)
  • 2020-11-28 19:22

    Try this code. It adds tab href to url + opens tab based on hash on page load:

    $(function(){
      var hash = window.location.hash;
      hash && $('ul.nav a[href="' + hash + '"]').tab('show');
    
      $('.nav-tabs a').click(function (e) {
        $(this).tab('show');
        var scrollmem = $('body').scrollTop() || $('html').scrollTop();
        window.location.hash = this.hash;
        $('html,body').scrollTop(scrollmem);
      });
    });
    
    0 讨论(0)
提交回复
热议问题