我在添加曲奇时有问题。读几个答案,但很难理解,如果你从来没有与他们合作过。
我想要的基本上是添加一个cookie,如果有人点击指定的按钮。例如,如果一个人点击了“喜欢”按钮,不管他是向前/后退还是刷新页面,隐藏的内容都会被显示出来,几天后cookie就会被删除。
我用来隐藏内容的内容如下:
HTML:
<div id="fd">
<p>Button from below will become active once you hit like button!</p>
<div id="get-it">
<a class="button"><img src="img/get-button.png"></a>
</div>
</div>
<div id='feedback' style='display:none'></div>javascript:
FB.Event.subscribe('edge.create', function (response) {
$('#feedback').fadeIn().html('<p>Thank you. You may proceed now!</p><br/><div id="get-it"><a class="button2" href="pick.html"><img src="img/get-button.png"></a></div>');
$('#fd').fadeOut();
});但是,如果我点击刷新或在内容页面上返回/转发,它将再次被隐藏。这就是为什么我想在点击按钮上添加一个cookie。有谁能给我一些解释或示例代码吗?
发布于 2012-04-27 17:31:11
我建议使用jQuery-cookie插件。下面是几个用法示例:
// Create a cookie
$.cookie('the_cookie', 'the_value');
// Create expiring cookie, 7 days from then:
$.cookie('the_cookie', 'the_value', { expires: 7 });
// Read a cookie
$.cookie('the_cookie'); // => 'the_value'
$.cookie('not_existing'); // => null
// EDIT
// Attaching to a button click (jQuery 1.7+) and set cookie
$("#idOfYourButton").on("click", function () {
$.cookie('the_cookie', 'the_value', { expires: 7 });
});
// Attaching to a button click (jQuery < 1.7) and set cookie
$("#idOfYourButton").click(function () {
$.cookie('the_cookie', 'the_value', { expires: 7 });
});还需要添加cookie是否存在的检查(用于重新加载浏览器时):
if ($.cookie('the_cookie')) {
// Apply rule you want to apply
$('.ClassToSelect').css("display", "none");
}发布于 2012-04-27 17:28:56
我建议您使用localStorage,而不是cookie。
https://stackoverflow.com/questions/10355164
复制相似问题