【发布时间】:2013-12-05 16:13:44
【问题描述】:
我的 js 应用程序是一个出现日历,用户可以在今天或之前打开一扇“门”,以便 div 背景图像发生变化。
我想设置一个 cookie,这样当他们第二天返回时,div 仍然有背景图像。
我已经设置了 cookie 和开门代码,但我不确定如何将 cookie 集成到开门功能中。任何帮助将不胜感激,非常感谢。
应用链接:http://p3.katecooperuk.com
门打开 - 从数组中选择随机背景图像:
$('.doors').click(function () {
if (today.getMonth() !== 11) {
return;
}
// Check if the date is tomorrow or later
if (+($(this).attr('id').split('dec')[1]) > day) {
// Show image telling user to come back
$(this).css('background-image', 'url(/images/come_back.png)');
return;
}
// Otherwise it is today or earlier
// Select Random Image
var doorImage = getRandomImage(calendarImg);
// Change background image of door that was clicked
$(this).css('background-image', doorImage);
});
饼干
//on document ready, checks if the cookie is set, and if so, sets the background with it's value
$(function(){
if($.cookie('background-image') != null){
$(this).css('background-image', $.cookie('background-image'));
}
});
// Set cookie
$(function() {
$(this).css('background-image', doorImage);
$.cookie('background-image', doorImage);
});
【问题讨论】:
标签: javascript cookies