2011-09-25 131 views
1

我正在为我的网站制作后台更改脚本。一切工作正常,除了应存储所需值的cookie。我正在使用jQuery 1.3。 IE 8说:'对象不支持第47行char 118567432'这个属性或方法'!?任何帮助将不胜感激。例如工作无饼干:通过cookie的jQuery自定义背景

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

例如用饼干(不工作):

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); 
+0

请添加一些信息,你会得到任何错误?安装FireBug for FireFox,该插件将显示任何错误。 – ChrisH

回答

2

我已经转换你的代码更高效和语法有效的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"); 
+0

谢谢,现在它工作。新问题是,如果我点击另一个页面的链接,它会给出默认的背景,而不是所选的。 – George

+0

我改变了我的答案,因为你的功能逻辑似乎有另一个星球的起源。阅读评论。 –

1

可能是您的“cookie插件”脚本导入不正确?你能否提供有关你得到的错误的更多细节。使用Firebug for Firefox或Chrome开发工具来获得更好的错误跟踪。