将jquery cookie添加到后台更换器

时间:2013-06-24 11:03:36

标签: jquery cookies jquery-plugins setcookie

嘿伙计们需要非常感谢帮助..我试图使用Jquery cookie插件向以下Jquery身体背景更换器代码添加cookie无济于事。代码工作正常,没有cookie来保留选择。

非常感谢大家。

jQuery('#stlChanger .stBgs a').click(function(){
    var bgBgCol = jQuery(this).attr('href');
    jQuery('#stlChanger .stBgs a').removeClass('current');
    jQuery('body').css({backgroundColor:'#ffffff'});
    jQuery(this).addClass('current');
    jQuery('body').css({backgroundImage:'url(' + bgBgCol + ')'});
    if (jQuery(this).hasClass('bg_t')){
        jQuery('body').css({backgroundRepeat:'repeat', backgroundPosition:'50% 0', backgroundAttachment:'scroll'});
    } else {
        jQuery('body').css({backgroundRepeat:'repeat', backgroundPosition:'50% 0', backgroundAttachment:'scroll'});
    }
    return false;
});

1 个答案:

答案 0 :(得分:0)

尝试

jQuery('#stlChanger .stBgs a').click(function(){
    var bgBgCol = jQuery(this).attr('href');
    jQuery('#stlChanger .stBgs a.current').removeClass('current');

    jQuery('body').css({backgroundColor:'#ffffff'});
    jQuery(this).addClass('current');
    jQuery('body').css({backgroundImage:'url(' + bgBgCol + ')'});

    //Assuming this is how the cookie value is set in your cookie plugin
    //We set the href value of the clicked element to the cookie, since it is the only identifier to identify the clicked element
    $.cookie('stlchanger-bg', bgBgCol)

    if (jQuery(this).hasClass('bg_t')){
        jQuery('body').css({backgroundRepeat:'repeat', backgroundPosition:'50% 0', backgroundAttachment:'scroll'});
    } else {
        jQuery('body').css({backgroundRepeat:'repeat', backgroundPosition:'50% 0', backgroundAttachment:'scroll'});
    }
    return false;
});

jQuery(function($){
    //When the page is reloaded the cookie value is read and we trigger a click event on the matched anchor element
    var bgBgCol = $.cookie('stlchanger-bg');
    if(bgBgCol){
        jQuery('#stlChanger .stBgs a[href="' + bgBgCol + '"]').trigger('click');
    }
})