你可以使用我写的这个非常简单的 cookie 处理对象:
var Cookie = {
set: function(name, value, days) {
if (days) {
var date = new Date();
date.setTime(date.getTime()+(days*24*60*60*1000));
var expires = "; expires="+date.toGMTString();
} else { var expires = ""; }
document.cookie = name + "=" + value + expires + "; path=" + location.pathname;
},
unset: function(name) { Cookie.set(name, "", -1); },
get: function(name) {
var search = new RegExp(name + "=([^;]*);");
var result = search.exec(document.cookie);
return result ? result[1] : undefined;
}
};
所以你的代码会变成:
$(document).ready(function(){
$('#colorpicker').colorPicker({
colorChange: function(e, ui) {
$('body').css({backgroundColor: ui.color});
Cookie.set('bgc', $('body').css('background-color'), 365);
}
});
$('#colorpicker').colorPicker('setColor', 0, 0, 6, 1);
if (Cookie.get('bgc')) {
$('body').css({ backgroundColor: Cookie.get('bgc') });
}
});