开发者

jQuery - ajax tabs and using cookies to remember active tab

I'm using jQuery to load content to a page via AJAX. I want to use cookies to remember which tab was last active, that way it will automatically load the proper page. My real question is ho开发者_开发知识库w can I use the jQuery cookies plugin to remember the last active tab, then apply the class 'active' (which is how I'm displaying the content) to the same tab on reload or other visits?


I guess something like this will work (a bit abstract - since there are dozens of tabs and cookie plugins out there):

This code should trigger on tab click or hover - or whatever else event you use:

$.cookie('activeTab', 'activeTabId');//save current tab id in activeTab cookie

then to get back to that tab when user reenters the page(this code should go into your $(document).ready(), or whatever else method you use to determine that DOM is ready):

if($.cookie('activeTab')){//checking if cookie set
    $('#' . $.cookie('activeTab')).addClass('active');
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜