jQuery通过cookie自定义背景

我正在为我的网站制作后台更改脚本。 除了应存储所需值的cookie之外,一切正常。 我正在使用jQuery 1.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示例(不工作):

 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代码。

 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插件”脚本未正确导入? 你能否详细说明你得到的错误。 使用Firebug for Firefox或Chrome Dev工具可以获得更好的错误跟踪。