简体   繁体   English

如果存在cookie,如何隐藏div

[英]How to hide a div if cookie exists

Looking to generate a cookie on a click of a button, then for it to check if the cookie exists, if the cookie exists then hide div. 希望通过单击按钮来生成cookie,然后对其进行检查以检查cookie是否存在,如果cookie存在,则隐藏div。

Here is where I am at with my code, but I cannot seem to get the if statement to function as intended.. 这是我的代码所在,但似乎无法使if语句按预期运行。

  (function ($) { $(".cookieSetter").click(function () { $.cookie('cookieMade', 'jobDone'); }); if ($.cookie('cookieMade', 'jobDone') == "true") { $('.box').hide(); } })(jQuery); 
  button { height: 48px; width: 112px; background: #fff; border: 2px solid #2d2d2d; color: #2d2d2d; font-size: 14px; font-weight: 600; text-transform: uppercase; cursor: pointer; } .blue { background: blue; } .box { width: 100px; height: 100px; margin-top: 10px; } 
  <a class="cookieSetter" href="#"><button>Set cookie</button></a> <div class="blue box"></div> 

Your syntax for checking the cookie isn't correct. 您检查Cookie的语法不正确。 You need to use the 'getter' for the value. 您需要使用“ getter”作为值。 Also note that you can use toggle() to show/hide the element as needed. 另外请注意,您可以根据需要使用toggle()显示/隐藏元素。 Try this: 尝试这个:

(function($) {
    $(".cookieSetter").click(function () {
        $.cookie('cookieMade', 'jobDone');
    });

    $('.box').toggle($.cookie('cookieMade') != 'jobDone');
})(jQuery);

Your usage of $.cookie('cookieMade', 'jobDone') is the setter of the cookie and not the getter. 您对$.cookie('cookieMade', 'jobDone')是cookie的设置者,而不是getter。 If you want to get the value of the cookie you should use $.cookie('cookieMade') , and check the returned value: 如果要获取cookie的值,则应使用$.cookie('cookieMade') ,并检查返回的值:

if ($.cookie('cookieMade') == 'jobDone') {
    // Do something if the cookie's value is 'jobDone'
}

In your case: 在您的情况下:

if ($.cookie('cookieMade') == 'jobDone') {
    // Do something if the cookie's value is 'jobDone'
    $('.box').hide();
}

update 更新

The problem with codepen is that each time you reload the page you get a different path , and by default, $.cookie sets the path of the cookie to the current path. Codepen的问题在于,每次重新加载页面时,您都会获得一个不同的path ,默认情况下, $.cookie 。cookie将cookie的路径设置为当前路径。
You can set the path to / so the cookie will be valid for all of the pages in your domain: 您可以将path设置为/以便Cookie对您域中的所有页面均有效:

$.cookie('cookieMade', 'jobDone', {path: '/'});

This will work in codepen (and should also work in your website). 这将在Codepen中工作(并且也应在您的网站中工作)。

When you set the cookie you can raise event and subscribe that event wherever is
needed.
(function ($) {  
  $(".cookieSetter").click(function () {
     $.cookie('cookieMade', 'jobDone');
    $(document).triggerHandler('_cookieSetter', true);
  });  
  $(document).off('_cookieSetter.add').on('_cookieSetter.add', function () {
               if ($.cookie('cookieMade') == 'jobDone') {
    $('.box').hide();
}});
})(jQuery);

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

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