通过cookie的jQuery自定义背景 [英] jQuery custom background through a cookie

查看:99
本文介绍了通过cookie的jQuery自定义背景的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在为我的网站制作后台更改脚本.除了应该存储所需值的cookie之外,其他所有东西都可以正常工作.我正在使用jQuery 1.3. IE 8说:对象在第47行上不支持此属性或方法char 118567432"!任何帮助将不胜感激.没有Cookie的工作示例:

I'm making background changing script for my site. Everything works fine except the cookie which should store the required value. I'm using jQuery 1.3. IE 8 says: 'object doesn't support this property or method on line 47 char 118567432'!? Any help will be greatly appreciated. Working example without cookie:

    function images(which,image) {
      if (which == 't1')image = images[0];
      else if (which == 't2')image = images[1];//and etc...
    }
    $('html').css("background-image", image);

带有Cookie的示例(不起作用):

Example with cookie (not working):

function images(which,image) {
          if (which == 't1')image = images[0];
            {$.cookie("html_img", "" + image + "", { expires: 7 });
            imgCookie = $.cookie("html_img");}
          else if (which == 't2')image = images[1];
            {$.cookie("html_img", "" + image + "", { expires: 7 });
            imgCookie = $.cookie("html_img");}
          }
        $('html').css("background-image", imgCookie);

推荐答案

我已将您的代码转换为更有效且在语法上有效的JavaScript代码.

I've converted your code to a more efficient, and syntactically valid JavaScript code.

function images(which){
    var image = /^t\d+/i.test(which) ? images[which.substr(1)-1] : null;
    if(image !== null) {
        $.cookie("html_img", image, {expires:7})
    } else {
        image = $.cookie("html_img");
        if(!image) image = images[0]; //If the image doesn't exist, use default=0
    }
    $('html').css("background-image", image);
}

$(document).ready(function(){
    images(); //Load image default from cookie
}

//If you want to change the image+cookie: images("t2");

这篇关于通过cookie的jQuery自定义背景的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆