gpt4 book ai didi

javascript - Cookie 添加按钮点击

转载 作者:塔克拉玛干 更新时间:2023-11-02 21:52:52 25 4
gpt4 key购买 nike

我在添加 cookie 时遇到问题。阅读几个答案,但如果您以前从未使用过它们,则很难理解。

我基本上想要的是在有人点击指定按钮时添加一个 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 的原因。谁能给我一些解释或示例代码?

最佳答案

我建议使用 jQuery-cookie plugin .下面是一些使用示例:

// 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");
}

关于javascript - Cookie 添加按钮点击,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10355164/

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