Twitter Bootstrap - Tabs - URL doesn't change

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

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

I have the following code:

15条回答
  •  情书的邮戳
    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
       });
    });
    

提交回复
热议问题