Twitter Bootstrap - Tabs - URL doesn't change

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

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

I have the following code:

15条回答
  •  失恋的感觉
    2020-11-28 19:24

    As your anchor elements already change the url hash, listening to onhashchange event is another good option. As @flori already mentioned, this method works nice with the browser back button.

    var tabs$ = $(".nav-tabs a");
    
    $( window ).on("hashchange", function() {
        var hash = window.location.hash, // get current hash
            menu_item$ = tabs$.filter('[href="' + hash + '"]'); // get the menu element
    
        menu_item$.tab("show"); // call bootstrap to show the tab
    }).trigger("hashchange");
    

    Finally, triggering the event just after you define the listener will also help showing the right tab on page load.

提交回复
热议问题