简体   繁体   English

如何记住使用jQuery.cookies和自定义jQuery标签代码的活动标签?

[英]How to remember active tab using jQuery.cookies and custom jQuery tabs code?

I have written this code to make quick tabs. 我已经编写了此代码以进行快速制表。 I need a way to remember active tab after refreshing page. 我需要一种刷新页面后记住活动标签的方法。

Here is my basic HTML code: 这是我的基本HTML代码:

<ul class="dashboard_tabs">
    <li><a href="#" rel="tab1">Tab 1</a></li>
    <li><a href="#" rel="tab2">Tab 2</a></li>
    <li><a href="#" rel="tab3">Tab 3</a></li>
    <li><a href="#" rel="tab4">Tab 4</a></li>
</ul>

<div class="dashboard_body" id="tab1">

</div>

<div class="dashboard_body" id="tab2">

</div>

<div class="dashboard_body" id="tab3">

</div>

<div class="dashboard_body" id="tab4">

</div>

I use rel relations to display the correct tab with this code: 我使用rel关系通过以下代码显示正确的标签:

jQuery(document).ready(function(){
    jQuery('.dashboard_body').css({display: 'none'});
    jQuery('.dashboard_tabs a:first').addClass('current');
    var rel = jQuery('.dashboard_tabs a.current').attr('rel');
    jQuery('#'+rel).show();

    jQuery('.dashboard_tabs a').click(function(){
        jQuery('.dashboard_tabs a').removeClass('current');
        jQuery('.dashboard_body').hide();
        jQuery(this).addClass('current');
        var rel = jQuery(this).attr('rel');
        jQuery('#'+rel).show();
    });
});

How can I use jQuery cookies to remember active tab? 如何使用jQuery cookie记住活动标签?

Just create/update the cookie in your click function 只需在点击功能中创建/更新Cookie

$.cookie('active_tab', rel);

You can then easily read the active tab from the cookie in your ready function and do whatever you want with CSS or alike. 然后,您可以在ready函数中轻松地从cookie中读取活动选项卡,并使用CSS或类似方法进行任何操作。

Look at this question: How to set/unset cookie with jQuery? 看一下这个问题: 如何使用jQuery设置/取消cookie? . You can use jquery-cookie plugin for easy manipulate with cookies. 您可以使用jquery-cookie插件轻松处理Cookie。 On document ready read rel from cookies and set correct tab. document ready请从cookie中读取rel信息并设置正确的标签。 On click event just save rel in cookies. click event只需将rel保存在cookie中即可。

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

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