Remember which tab was active after refresh

前端 未结 17 2169
眼角桃花
眼角桃花 2020-11-27 04:43

I\'m using jquery tabs on a web page and when the page is refreshed it loses what ever tab I had been on and goes back to the first tab.

Has anyone come across this

17条回答
  •  轻奢々
    轻奢々 (楼主)
    2020-11-27 05:00

    I've recently had the same problem and spent a longtime looking at the docs for JQuery UI Tabs Widget. I ended up using the events activate and create for JQuery UI 1.10 as well as localStorage to store the current tab before page refresh.

        $( ".selector" ).tabs({                                                 
            activate: function( event, ui) {
                //when tab is activated store it's index in activeTabIndex
                    var activeTabIndex = $('.tabs').tabs('option','active');
                //add activeTabIndex to localStorage and set with key of 'currentTab'  
                    localStorage.setItem('currentTab', activeTabIndex); 
                                },
                create: function( event, ui ) {
                    $(".tabs").tabs({       
                //when tabs are created on page refresh, the active tab is set to index of 'currentTab' 
                //after getItem from localStorage
                active: localStorage.getItem('currentTab')
            });
          }
       });
    

提交回复
热议问题