Jquery - 加载cookie数据

发布于 2024-12-06 04:35:04 字数 797 浏览 1 评论 0原文

考虑一个缩略图列表,用户单击其中的一个即可更改背景图像。以下脚本允许用户选择背景图像并应用它。

$(function() {
        $('.themes li a img').click(function() {
            var image = 'imgs/someimage.jpg';
            //Set cookie
            function setCookie(){
                $.cookie("html_img", image, {expires:7});
                var a = $.cookie('html_img');//get cookie value
                return a;
            }
       });
});

当用户访问另一个页面时,另一个名为 loadCookie.js 的脚本会加载包含所选背景的 cookie:

$(function() {
        var startImage='';
        startImage = setCookie();
        $(document).ready(function(){
            $('html').css("background", startImage);
        });
    });

IE8 说“对象不支持此操作,第 3 行”; Jquery cookie 插件已安装。我知道有一些我找不到的小错误。非常感谢任何使脚本工作的帮助。

Consider a list of thumbnails from which the user clicks one to change the background image. The following scripts lets the user choose a background image and apply it.

$(function() {
        $('.themes li a img').click(function() {
            var image = 'imgs/someimage.jpg';
            //Set cookie
            function setCookie(){
                $.cookie("html_img", image, {expires:7});
                var a = $.cookie('html_img');//get cookie value
                return a;
            }
       });
});

Another script called loadCookie.js loads the cookie which contains the chosen background when the user visits another page:

$(function() {
        var startImage='';
        startImage = setCookie();
        $(document).ready(function(){
            $('html').css("background", startImage);
        });
    });

IE8 says 'object doesn't support this action, line 3'; Jquery cookie plugin is installed. I know there is some little mistake I can't find. Any help making the script work is greatly appreciated.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

笑看君怀她人 2024-12-13 04:35:04

不是 100% 明白你想要做什么,但我认为这就是你想要的(假设你正在使用 jquery.cookie.js):

$(function() {

    if($.cookie("html_img")) {
        $('html').css("background", $.cookie("html_img"));
    }

    $('.themes li a img').click(function() {

        var image = 'imgs/someimage.jpg';
        // var image = $(this).attr('src');

        $('html').css("background", image);

        $.cookie("html_img", image, {expires:7});

        return false;

    });

});

Not 100% on what you're trying to do, but I think this is what you're after (assuming you're using jquery.cookie.js):

$(function() {

    if($.cookie("html_img")) {
        $('html').css("background", $.cookie("html_img"));
    }

    $('.themes li a img').click(function() {

        var image = 'imgs/someimage.jpg';
        // var image = $(this).attr('src');

        $('html').css("background", image);

        $.cookie("html_img", image, {expires:7});

        return false;

    });

});
紅太極 2024-12-13 04:35:04

由于这是您针对同一代码发布的第三个问题(之前的问题此处此处),您似乎对这段代码和使用堆栈溢出的正确方法感到非常困惑。

以下是 cookie 的一般工作原理。在一页上,您设置了一个 cookie。在后续页面上,您将读取该 cookie 并使用它的值。

在此代码示例中,您将第一段代码中的 cookie 设置为背景图像。到目前为止,一切都很好。

在第二个代码块中,您再次调用 setCookie。如果你想从cookie中检索背景图像,那么你需要获取cookie值,而不是设置它。您的代码可能应该是这样的:

$(document).ready(function() {
    // initialize background from cookie
    var startImage = $.cookie("html_img");   // get cookie value
    if (startImage) {
        $(document.body).css("background-image", startImage);
    }

    // install click handler to change background
    $('.themes li a img').click(function() {
        var newBackground = 'imgs/someimage.jpg';
        $(document.body).css("background-image", newBackground);
        $.cookie("html_img", newBackground, {expires:7});  // save in cookie
   });
});

您可能还需要知道,如果您没有为 cookie 指定路径选项,例如 path: '/',则 cookie 将只能在当前路径而不是您网站上的其他位置。该 jQuery 附加 cookie 库的文档位于此处

至于使用 stackoverflow,您应该就特定的代码片段提出一个明确的问题,当受访者感到困惑或提供的答案不是您想要问的内容时,您应该适当地澄清您的问题,直到获得适当的答案。请记住,当人们提供错误的答案时,很可能是因为他们不明白你在问什么,而不是因为他们不知道如何回答。您不应该在新问题中再次发布同一问题的微小变化。

Since this is your third question posted about this same code (previous ones here and here), you seem really confused both about this code and about the proper way to use stack overflow.

Here's how cookies work in general. On one page, you set a cookie. On a follow-on page you read that cookie and use it's value.

In this code example, you are setting a cookie in the first block of code to a background image. So far, so good.

In the second block of code, you are calling setCookie again. If you want to retrieve the background image from the cookie, then you need to get the cookie value, not set it. Your code should probably be something like this:

$(document).ready(function() {
    // initialize background from cookie
    var startImage = $.cookie("html_img");   // get cookie value
    if (startImage) {
        $(document.body).css("background-image", startImage);
    }

    // install click handler to change background
    $('.themes li a img').click(function() {
        var newBackground = 'imgs/someimage.jpg';
        $(document.body).css("background-image", newBackground);
        $.cookie("html_img", newBackground, {expires:7});  // save in cookie
   });
});

You may also need to know that if you do not specify a path option for the cookie like path: '/', the cookie will only be readable on the current path and not elsewhere on your site. Documentation for that jQuery add-on cookie library is here.

As for using stackoverflow, you should ask a clear question about a particular piece of code and when respondents are confused or provide answers that aren't what you meant to ask, you should clarify your question appropriately until you get an appropriate answer. Remember, when people provide answers in the wrong direction, it is most likely because they don't understand what you are asking, not because they don't know how to answer. You should not post a minor variation of the same question all over again in a new question.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文