gpt4 book ai didi

jquery - 使用 cookie 永久更改 CSS

转载 作者:行者123 更新时间:2023-11-28 09:39:23 25 4
gpt4 key购买 nike

我正在制作一个带有一些 jQuery 实验(有点花哨)的小网站。在每场比赛中都有一个目标,当找到并点击目标时,我想对页脚中的心形符号进行永久更改。我正尝试为此使用 cookie 插件。

指向其中一个子页面的链接:http://www.carlpapworth.com/htmlove/arrows.html

这是页脚 CSS:

footer{
position: fixed;
bottom: 0px;
padding: 10px;
width: 100%;
height: 100px;
background: /*url(../images/bgFooter.png)*/ #dddddd;
z-index: 2000;
}

.heartCollection{
width: 940px;
margin: 0 auto;
text-align: justify;
}

.heartCollection p{
font-size: 13px;
float: none;
width: 100%;
padding: 0;
margin: 0 0 -20px 0;
text-align: center;
position: relative;
}

.heartCollection ul li{
width: auto;
display: inline;
list-style: none;
float: left;
margin: 10px 0 -10px 0;
padding: 0 0 0 98px;
font-size: 70px;
}

.heartCollection ul li a{
font-family: menlo;
color: #cccccc;
}

.found{
color: #ff63ff;
}

.credits{
width: 100%;
height: auto;
margin: 80px auto;
bottom: 0px;
left: -40px;
position: relative;
text-align: right;
}

这是 Javascript:

$(document).ready(function() {
//help
$('#helpInfo').hide();
$('#help h2').click(function(){
$('#helpInfo').show(300);
});
$('#helpInfo').click(function() {
$(this).hide(300);
});
//reward
$('#reward').hide();
$('#goal a').click(function(){
$('#reward').fadeIn(1000);
});
//Collection
$.cookie('class','found',{
});
var foundHeart = $.cookie('found');
$('.exit').click(function(){
$('#collection1').addClass(foundHeart);
});

});

什么都没发生,那我做错了什么?编辑:更重要的是,我应该怎么做才能修复它?

最佳答案

有两处错误:

首先,

var foundHeart = $.cookie('found');

您正在尝试使用上述函数按名称检索 cookie。相反,您传递的是值(value)。

cookie 参数设置如下:

$.cookie('name', 'value', { options });

因此您的 cookie 的名称是“class”,值是“found”。

换句话说

var foundHeart = $.cookie('found');

应该是

var foundHeart = $.cookie('class');

其次,即使您更正您的代码也不会按预期运行。为什么?因为您要在加载时设置 cookie。

此行设置 cookie:

$.cookie('name', 'value');

但是您正在文档就绪函数中运行它。

您应该将该行移动到此函数中:

$('#goal a').click(function(){
$('#reward').fadeIn(1000);
// moved set cookie function here
$.cookie('class', 'found');
});

这样它只会在您达到目标时设置。

关于jquery - 使用 cookie 永久更改 CSS,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12969086/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com