gpt4 book ai didi

javascript - JQuery Cookie 不起作用

转载 作者:行者123 更新时间:2023-11-30 12:28:55 24 4
gpt4 key购买 nike

我有一个 javascript 函数 showHide(shID) ,这是一个隐藏 div 并在单击“阅读更多”后显示内容的函数。这是我的 fiddle :Fiddle

在这个函数中添加了 JQuery Cookie 之后,它的函数 showHide(shID) 似乎消失了,谁能帮我解决这个问题?谢谢我真的需要帮助。这是我的 fiddle :Fiddle

我需要像这样做 smtg :首先点击“阅读更多”,隐藏的内容会显示,设置 cookie 后用户回来访问我的页面隐藏的内容仍然显示。

<div id="wrap">
<a href="#" id="example-show" class="showLink" onclick="showHide('example');">
<button class="btn-u btn-u-lg btn-block btn-u-dark-orange">
Read More
</button>
</a>
<div id="example" class="more">
<iframe width="600" height="315" src="//www.youtube.com/embed/BaPlMMlyM_0" frameborder="0" allowfullscreen></iframe>
<p>Congratulations! You've found the magic hidden text! Clicking the link below will hide this content again.</p>
</div>
</div>

最佳答案

我稍微更改了脚本逻辑:当showhide-... 设置为1 时显示内容。我还向 showHide 函数添加了一个参数:setCookie。当这是 false 时,cookie 没有设置/更改。

function showHide(setCookie) {
var shID = $(this).data("showhide")
, $shEl = $("#" + shID)
, $showHide = $("#" + shID + '-show')
;

if ($shEl.is(":hidden")) {
if (setCookie !== false) {
jQuery.cookie("showhide-" + shID, 1);
}
$showHide.hide();
$shEl.show();
} else {
if (setCookie !== false) {
jQuery.cookie("showhide-" + shID, 0);
}
$showHide.show();
$shEl.hide();
}
}

jQuery(document).ready(function () {
$("#example-show").on("click", showHide);
if (jQuery.cookie("showhide-" + "example") == '1') {
showHide.call($("#example-show").get(0), false);
}
});

要添加过期日期,只需将第三个参数传递给 $.cookie 调用:

var date = new Date();
var minutes = 30;
date.setTime(date.getTime() + (minutes * 60 * 1000));
$.cookie("example", "foo", { expires: date });

(有关更多信息,请参阅 How to expire a cookie in 30 minutes using jQuery?)

JSFIDDLE

关于javascript - JQuery Cookie 不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28346209/

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