【问题标题】:Why is this JavaScript cookie not working? [closed]为什么这个 JavaScript cookie 不起作用? [关闭]
【发布时间】:2012-06-25 16:53:28
【问题描述】:

我知道这段代码可以正常工作,在重新排列包含其他代码的 div 元素时,我一定是意外更改了某些内容,因此它现在似乎无法正常工作。如果有人对此有所了解,我将不胜感激。我需要“显示:无;”在窗口 div 上?抱歉我不记得了。

<script type="text/javascript">
    $(document).ready(function() {
     // If the 'hide cookie is not set we show the message
      if (!readCookie('hide')) {
        $('#window').show();
      }
      // Add the event that closes the popup and sets the cookie that tells us to
      // not show it again until one day has passed.
      $('#close').click(function() {
        $('#window').hide();
        createCookie('hide', true, 1)
        return false;
      });
    });
    // ---
    // And some generic cookie logic
    // ---
    function createCookie(name,value,days) {
      if (days) {
        var date = new Date();
        date.setTime(date.getTime()+(days*24*60*60*1000));
        var expires = "; expires="+date.toGMTString();
      }
      else var expires = "";
      document.cookie = name+"="+value+expires+"; path=/";
    }
    function readCookie(name) {
      var nameEQ = name + "=";
      var ca = document.cookie.split(';');
      for(var i=0;i &amp;amp;amp;amp;amp;amp;amp;amp;lt; ca.length;i++) {
        var c = ca[i];
        while (c.charAt(0)==' ') c = c.substring(1,c.length);
        if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
      }
      return null;
    }
    function eraseCookie(name) {
      createCookie(name,"",-1);
    }
    </script>

【问题讨论】:

  • 不起作用如何?没设置好?读不出来?被读取但不隐藏/显示div?
  • 这到底是什么for(var i=0;i &amp;amp;amp;amp;amp;amp;amp;amp;amp;lt; ca.length;i++)
  • 大声笑,也许代码被实体编码了:D
  • :( 我刚看到,我的 html 编辑器一定是这样做的。我不知道那里有什么 :( 抱歉。
  • 好吧,如果这实际上在您的源代码中,那可能就是问题所在。我不明白的是为什么您在 JS 控制台中没有看到任何错误

标签: javascript jquery html css cookies


【解决方案1】:

你的 for 循环条件有问题。

应该是

    for(var i=0;i<ca.length;i++)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2016-12-22
    • 1970-01-01
    • 2017-12-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-09-12
    相关资源
    最近更新 更多