问题描述
我有一个javascript函数showHide(shID),这是一个功能,以隐藏div和点击阅读更多后显示内容。这是我的小提琴:
I had a javascript Function showHide(shID) , which is a function to hide div and show content after clicked "read more" . Here is my Fiddle : Fiddle
添加JQuery Cookie到这个函数后,它看起来像函数showHide(shID)走了,任何人都可以帮我修复它吗?谢谢我真的需要帮助。这里是我的小提琴:
after added JQuery Cookie into this function , its seem like function showHide(shID) was gone , can anyone help me to fix it ? thanks I really need help . here my Fiddle : Fiddle
我需要做smtg像这样:首先点击readmore,隐藏的内容将显示,设置cookie后,用户回来访问我的页面隐藏的内容仍然显示。
I need to do smtg like this :First click on "readmore", hidden content will show,after set cookie and user come back to visit my page hidden content still showing .
<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。
I changed the script logic a little: show the content when showhide-...
is set to 1
. Also I added a parameter to the showHide
function: setCookie
. When this is false
, the cookie is no set/changed.
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
调用:
To add an expire date, just pass the third argument to the $.cookie
call:
var date = new Date();
var minutes = 30;
date.setTime(date.getTime() + (minutes * 60 * 1000));
$.cookie("example", "foo", { expires: date });
(请参阅
这篇关于JQuery Cookie不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!