2015-02-05 68 views
1

我有一个javascript函数showHide(shID),它是一个隐藏div并在点击“read more”后显示内容的函数。这里是我的小提琴:FiddleJQuery Cookie不起作用

在将JQuery Cookie添加到此函数后,它看起来像函数showHide(shID)消失了,任何人都可以帮我修复它吗?谢谢我真的需要帮助。这里我的小提琴:Fiddle

我需要这样做smtg:首先点击“readmore”,隐藏内容会显示,设置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> 
+0

JQuery的饼干 - 这是插入。你需要单独包括它。纠正我,如果我错了 – demo 2015-02-05 14:23:12

+0

嗨演示,谢谢你回复我,我也不太确定,我第一次使用JQuery cookie – lucas 2015-02-05 14:38:53

+0

请看这个http://plugins.jquery.com/cookie/我认为你需要下载并添加库到项目 – demo 2015-02-05 14:41:47

回答

3

我改剧本逻辑一点:显示内容时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

+0

嗨Ionica Bizau,非常感谢您的帮助!这对我来说很好!但是,如何为此设置1天过期日期?谢谢 ! = D – lucas 2015-02-06 08:12:29

+0

@lucas酷。查看编辑。 – 2015-02-06 08:14:33

+0

我对吗?如果我这样写的话,在测试http://jsfiddle.net/Garfrey/3Lzytvqy/4/时放1分钟,我应该在“foo”中改变什么。 – lucas 2015-02-06 08:24:45

0

Cookie的名称区分大小写,您必须阅读您之前设置的cookie的相同名称。在你的情况下,你设置 jQuery.cookie("showHide-"+shID, 1)并检查jQuery.cookie("showhide-" + "example")。 “showHide-”和“showhide-”不匹配。

编辑:

if (document.getElementById(shID + '-show').style.display != 'none') { 
     // Set cookie when button has been clicked 
     jQuery.cookie("showhide-"+shID, 1); 
     // ... 
    } else { 
     jQuery.cookie("showhide-"+shID, 0); 
     // ... 
    } 

    if (jQuery.cookie("showhide-" + "example") == 1) { 
     // Trigger button click if cookie is set 
     $("#example-show").trigger("click"); 
    } 
+0

嗨rba,感谢您的回复,意味着我只需要把“showhide”放在“showHide”的权利?我曾试过这个,然后才工作 – lucas 2015-02-05 15:27:43

+0

也许你只是需要放置你的按钮触发器,当你设置cookie时,它是相反的。从你的答案看来,你似乎想“设置cookie和用户回来访问我的页面隐藏的内容仍然显示”,并在您的代码中尝试“//Cookie没有设置所以显示div”的评论。 – rba 2015-02-05 16:05:32

+0

是的,我需要“设置cookie和用户回来访问我的页面隐藏的内容仍然显示”...对不起,我不是很明白,你能为我做一个小提琴为例吗?我需要为我的方案项目,我也是新的JavaScript ..谢谢rba, – lucas 2015-02-05 16:36:03