简体   繁体   English

使用Cookie的JavaScript显示/隐藏框

[英]JavaScript show/hide box using cookies

Hello I have a small problem here with my JavaScript. 您好,我的JavaScript有一个小问题。

  $(document).ready(function() { var status = getCookie(); if (status > 0) { setCookie(1); document.getElementById('cookiemenu_dropdown').style.display = "block"; } else { setCookie(0); document.getElementById('cookiemenu_dropdown').style.display = "none"; } }); //----------------------------------------------------- function getCookie() { var cookies = document.cookie.split(';'); var FirstCookie = cookies[0]; var FirstValue = FirstCookie.split('=')[1]; return FirstValue; } //----------------------------------------------------- function setCookie(value) { var expires = new Date(); expires.setMonth(expires.getYear() + 1); document.cookie = 'divStav' + '=' + value + ';EXPIRES=' + expires.toGMTString(); } //----------------------------------------------------- function ShowHideCookieBox() { var display = document.getElementById('cookiemenu_dropdown').style.display; if (display == "block") { document.getElementById('cookiemenu_dropdown').style.display = "none"; setCookie(0); } else { document.getElementById('cookiemenu_dropdown').style.display = "block"; setCookie(1); } } 
 <div id='cookiemenu'> <div class='cookiemenu_header' onclick='ShowHideCookieBox()'> <img src='./img/triangle.png' />Letní akce! <img src='./img/triangle.png' /> </div> <div id="cookiemenu_dropdown" class='cookiemenu_content'> <span class='date'>Lorem</span> Lorem ipsum dolor sit amet, consectetur adipiscing elit. Phasellus mollis magna sed scelerisque hendrerit. Curabitur non quam bibendum, eleifend lorem et, bibendum turpis. Aliquam finibus velit ac lorem consectetur hendrerit. Integer viverra risus vitae sapien </div> </div> 

Script should work like this: 脚本应该像这样工作:

When I visit my web page first time, box is hidden (by default). 当我第一次访问我的网页时,该框是隐藏的(默认情况下)。 When I click on header, the box shows up and the value 1 is set in a cookie (there is just one cookie on my web). 当我单击标题时,将显示该框,并且在cookie中设置了值1(我的网站上只有一个cookie)。 When I visit again, that box should be opened (from cookie JavaScript takes value 1 and when the value is 1, the display:block is set). 当我再次访问时,应打开该框(从cookie JavaScript中获取值1,并且当该值为1时,将设置display:block )。

There is the problem. 有问题。 Clicking and showing/hiding works fine. 单击并显示/隐藏可以正常工作。 The box is hidden when I visit page first time (this is OK too). 第一次访问页面时,该框是隐藏的(也可以)。 But cookies aren't saving. 但是cookie不能保存。 Then I let box open and press F5, box is hidden after page reload. 然后我打开框并按F5键,重新加载页面后框被隐藏。

Could someone help me with that? 有人可以帮我吗?

Works fine for me. 对我来说很好。 Did you include jQuery? 您是否包含jQuery? If you want to do it pure javascript way you can change your ready function like: 如果您想使用纯JavaScript方式进行操作,则可以更改现成的功能,例如:

window.onload = function() {
      var status = getCookie();
      if (status > 0) {
        setCookie(1);
        document.getElementById('cookiemenu_dropdown').style.display = "block";
      } else {
        setCookie(0);
        document.getElementById('cookiemenu_dropdown').style.display = "none";
      }
    };

You got year and set it to month, so you broke expire date: 您获得了年份并将其设置为月,因此您违反了到期日期:

expires.setMonth(expires.getYear() + 1);

Try to use setYear instead of setMonth . 尝试使用setYear而不是setMonth

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM