Javascript jQuery通过cookie自定义背景

Javascript jQuery通过cookie自定义背景,javascript,jquery,cookies,background,Javascript,Jquery,Cookies,Background,我正在为我的网站制作背景更改脚本。除了存储所需值的cookie之外,其他一切都可以正常工作。我正在使用jquery1.3。IE 8说:“对象不支持第47行char 118567432上的这个属性或方法!”!?任何帮助都将不胜感激。没有cookie的工作示例: function images(which,image) { if (which == 't1')image = images[0]; else if (which == 't2')image = image

我正在为我的网站制作背景更改脚本。除了存储所需值的cookie之外,其他一切都可以正常工作。我正在使用jquery1.3。IE 8说:“对象不支持第47行char 118567432上的这个属性或方法!”!?任何帮助都将不胜感激。没有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示例(不工作):


我已经将您的代码转换为更高效、语法更有效的JavaScript代码

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插件”脚本未正确导入?你能详细说明你所犯的错误吗。使用Firefox Firebug或Chrome开发工具获得更好的错误跟踪。

请添加一些信息,您是否收到任何错误?安装FireFox的FireBug,该插件将显示任何错误。谢谢,现在它可以工作了。新的问题是,如果我点击另一个页面的链接,它会给出默认的背景,而不是所选的。我已经更改了我的答案,因为你的函数逻辑似乎来自另一个星球。阅读评论。
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");