Remember which tab was active after refresh

前端 未结 17 2170
眼角桃花
眼角桃花 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:08

    Here is an alternative way to allow remembering the open tab by the element id (not index). This is useful, if you use this code to synchronize the open tabs on two different pages with similar elements (like show and edit page).

    var tabsid = "#tabs";
    var cookieid = "tabs_selected2";
    
    var activetabnr = 0;
    if (($.cookie(cookieid)!=null) && ($.cookie(cookieid)!="")) {
       activetabnr = $(tabsid+' a[href="'+$.cookie(cookieid)+'"]').parent().index();
    }
    
    $(tabsid).tabs({
       active: $(tabsid).tabs({ active: activetabnr }),
       beforeActivate: function (event, ui) {
            $.cookie(cookieid, "#"+ui.newPanel.attr('id'));
       }
    });
    

    Works with jQuery UI 1.10. Do not forget to include the jquery.cookie plugin!

    
    

提交回复
热议问题