简体   繁体   English

jQuery-Ajax标签并使用cookie记住活动标签

[英]jQuery - ajax tabs and using cookies to remember active tab

I'm using jQuery to load content to a page via AJAX. 我正在使用jQuery通过AJAX将内容加载到页面。 I want to use cookies to remember which tab was last active, that way it will automatically load the proper page. 我想使用cookie来记住哪个选项卡最后一次处于活动状态,这样它将自动加载正确的页面。 My real question is how 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? 我真正的问题是如何使用jQuery cookie插件记住上一个活动选项卡,然后在重新加载或其他访问时将“活动”类(即我如何显示内容)应用于同一选项卡?

I guess something like this will work (a bit abstract - since there are dozens of tabs and cookie plugins out there): 我猜类似的东西会起作用(有点抽象-因为那里有很多选项卡和cookie插件):

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): 然后在用户重新进入页面时返回到该选项卡(此代码应放入$(document).ready()或用于确定DOM准备就绪的任何其他方法):

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM