【问题标题】:Jquery tabs cookie use existing scriptJquery tabs cookie 使用现有脚本
【发布时间】:2012-04-10 03:05:32
【问题描述】:

可能这并不难做到,但对于我来说,JS 背景并不难理解。我正在尝试将选定的选项卡存储在 cookie 中,因此如果页面仍然刷新,它将更早地显示选定的选项卡。

我已经在为我的网站使用列表网格布局,并且设置了 cookie 并且工作正常。我正在发布我正在使用的 cookie 代码以及我的标签 html 和 javascript。

列表/网格 Cookie JS

    $(function() {
        var cc = $.cookie('list_grid');
        if (cc == 'g') {
            $('#listgrid').addClass('grid');
            $('#grid').addClass('current');            
        } else {
            $('#listgrid').removeClass('grid');
            $('#list').addClass('current');
        }
    });

$(document).ready(function() {

    $('#grid').click(function() {
        $(".current").removeClass("current");
        $(this).addClass("current");

        $('#listgrid').fadeOut(500, function() {
            $(this).addClass('grid').fadeIn(500);
            $.cookie('list_grid', 'g');
        });
        return false;
    });

    $('#list').click(function() {
        $(".current").removeClass("current");
        $(this).addClass("current");

        $('#listgrid').fadeOut(500, function() {
            $(this).removeClass('grid').fadeIn(500);
            $.cookie('list_grid', null);
        });
        return false;
    });

});

我的标签页 HTML

  • 标签 1
  • 标签 2
  • 标签 3
  • <div class="tabs">
    
        <div id="tab1" class="tab">
        </div>
    
        <div id="tab2" class="tab">
        </div>
    
        <div id="tab3" class="tab">
        </div>
    </div>
    

    标签 JS jQuery(文档).ready(函数(){ //如果这不是第一个选项卡,则隐藏它 jQuery(".tab:not(:first)").hide();

        //to fix u know who
        jQuery(".tab:first").show();
    
        //when we click one of the tabs
        jQuery(".htabs a").click(function () {
    
            $(".current").removeClass("current");
            $(this).addClass("current");
    
            //get the ID of the element we need to show
            stringref = jQuery(this).attr("href").split('#')[1];
            //hide the tabs that doesn't match the ID
            jQuery('.tab:not(#' + stringref + ')').hide();
            //fix
            if (jQuery.browser.msie && jQuery.browser.version.substr(0, 3) == "6.0") {
                jQuery('.tab#' + stringref).show();
            } else
            //display our tab fading it in
            jQuery('.tab#' + stringref).fadeIn();
            //stay with me
            return false;
    
        });
    
    });
    

    那么任何人都可以帮助我做到这一点。

    【问题讨论】:

    • 您是在尝试编写自己的选项卡还是使用现有的 jQuery UI?
    • 我想要做的是,我正在使用从任何教程下载的用于列表/网格布局的 cookie 脚本。请参阅ssdtutorials.com/tutorials/series/…,所以我想为我的标签 UI 使用相同的 cookie 脚本。我不知道它是否可能

    标签: jquery cookies tabs refresh


    【解决方案1】:

    我认为你最好使用 this 的 jquery ui 选项卡

    jQuery(document).ready(function () { 
    
    // get the cookie
     var tabcookie = $.cookie('tab');    
     if (tabcookie){
         jQuery('.tab:not(#' + tabcookie + ')').hide();
         jQuery('.tab#' + tabcookie ).show();    
     }else{
          jQuery(".tab:not(:first)").hide();
    
        //to fix u know who
        jQuery(".tab:first").show();
     }
    
    
    //when we click one of the tabs
    jQuery(".htabs a").click(function () {
    
        $(".current").removeClass("current");
        $(this).addClass("current");
    
        //get the ID of the element we need to show
        stringref = jQuery(this).attr("href").split('#')[1];        
        //hide the tabs that doesn't match the ID
        jQuery('.tab:not(#' + stringref + ')').hide();
    
        // save the cookie
        $.cookie('tab', stringref);
    
    
        //fix
        if (jQuery.browser.msie && jQuery.browser.version.substr(0, 3) == "6.0") {
            jQuery('.tab#' + stringref).show();
        } else
        //display our tab fading it in
        jQuery('.tab#' + stringref).fadeIn();
        //stay with me
        return false;
    
    });
    

    });​

    【讨论】:

    • 我已经尝试过了,但不知何故它不起作用,因为 HTML 标记与我的有点不同。
    • 我已经尝试过你的代码,但它甚至没有同时隐藏我的所有 div 显示:(如果你愿意,如果你能找到我的错,我可以给你我的所有文件。
    • 我认为你已经接近了,我只是想表明你应该在标签点击事件上将标签 # 保存在 cookie 中(我命名为“标签”)。然后您可以在文档加载时引用该 cookie (jQuery(document).ready) 来决定要显示哪个选项卡。
    • 戴夫感谢您的帮助和努力,但我不是 Javascript 领域的成员,所以它过头了。如果你能做到,我需要一些具体的帮助,我真的很感激。
    • 我把我的解决方案here。确保您有对 jquery cookie 插件的引用。(我假设您这样做是因为您在网格/列表示例中使用它。
    猜你喜欢
    • 2014-11-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-04-16
    • 1970-01-01
    • 2012-03-06
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多