1

嘿,伙计们需要非常感谢的帮助。我尝试使用 Jquery cookie 插件将 cookie 添加到以下 Jquery 正文背景更改器代码中,但无济于事。该代码工作正常,只是没有 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;
});
4

1 回答 1

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');
    }
})
于 2013-06-24T11:09:14.170 回答