设置和读取cookie

时间:2014-12-12 14:57:25

标签: jquery cookies

我使用以下jquery代码来设置我的个人资料图片。但我想将它存储在一个cookie中。但无论我尝试什么,它都不会奏效。有人可以帮忙吗?

设置var bgCover = $ .cookie(" bgCover");没有工作

function setCover() {

            var profileContainer = $('#coverHeader'),
                coverContainer = $('#coverGallery'),
                coverThumbBox = $('.gr-item');


            $('.use-cover').click(function () {
                var bgCover = $(this).closest(coverThumbBox).data("src"),
                    elem = $(this).closest(coverThumbBox);

                // Add text style to header. Find cat_dark or cat_light in to item

                if ((elem).hasClass('cat_dark')) {
                    profileContainer.removeClass('theme-inverse').addClass('theme-default');
                }
                else {
                    profileContainer.removeClass('theme-default').addClass('theme-inverse');
                }

                elem.addClass('active').siblings().removeClass('active');


                // Set backround to profile header

              profileContainer.css("background-image", "url(/themes/site_themes/dashboard/img/cover/full/" + bgCover + ".jpg)");

                return false;
            });

            // remove backround to profile header
            $('.reset-cover').click(function () {
                profileContainer.removeClass('theme-default theme-inverse').css("background-image", "");
                coverContainer.find(coverThumbBox, '.active').removeClass('active')
            });

        }
      setCover();

添加到var:

            bgCover = $.cookie("bgCover");

在profileContainer.css上更改:

            $.cookie("bgCover", bgCover, { expires: 7 });
            profileContainer.css("background-image", "url(/themes/site_themes/dashboard/img/cover/full/" + $.cookie("bgCover") + ".jpg)");

没有工作。

1 个答案:

答案 0 :(得分:0)

解决了......

       function setCover() {

            var profileContainer = $('#coverHeader'),
                coverContainer = $('#coverGallery'),
                coverThumbBox = $('.gr-item'),
                bgCover = $.cookie("bgCover");

            $('.use-cover').click(function () {
                var bgCover = $(this).closest(coverThumbBox).data("src"),
                    elem = $(this).closest(coverThumbBox);

                // Add text style to header. Find cat_dark or cat_light in to item

                if ((elem).hasClass('cat_dark')) {
                    profileContainer.removeClass('theme-inverse').addClass('theme-default');
                }
                else {
                    profileContainer.removeClass('theme-default').addClass('theme-inverse');
                }

                elem.addClass('active').siblings().removeClass('active');


                // Set backround to profile header
               $.cookie("bgCover", bgCover);
               profileContainer.css("background-image", "url(/themes/site_themes/dashboard/img/cover/full/" + bgCover + ".jpg)");

                return false;
            });

            // remove backround to profile header
            $('.reset-cover').click(function () {
            $.cookie('bgCover', null);
                profileContainer.removeClass('theme-default theme-inverse').css("background-image", "url(/themes/site_themes/dashboard/img/cover/full/cover50.jpg)");
                coverContainer.find(coverThumbBox, '.active').removeClass('active')
            });

        }
      setCover();


       $(function() {
       if($.cookie('bgCover') == null) {
       $("#coverHeader").css("background-image", "url(/themes/site_themes/dashboard/img/cover/full/cover50.jpg)");
      } else {
       $("#coverHeader").css("background-image", "url(/themes/site_themes/dashboard/img/cover/full/" + $.cookie('bgCover') + ".jpg)");
      }
      });