为什么我的"用cookie显示/隐藏" jquery代码不工作?

时间:2017-12-13 10:00:04

标签: jquery cookies

我网站上的任何页面都有一个灰色div中的消息(#showHideInfoXmas):https://www.bagnboxman.co.uk/product/gusseted-clear-bottle-bags-94-diam-x-420mm/

它没有用!

我希望允许用户关闭邮件并让网站记住他们的选择。

我已经包含了cookie jquery插件 - 我甚至尝试将脚本移动到include之后。

$(function() {
  $('#showHideInfoXmasButton').parent().toggle(!$.cookie('divHidden')); // Hide if cookie there
  $('#showHideInfoXmasButton').click(function() {
    $(this).parent().hide();
    $.cookie('divHidden', 'true'); // Remember it was hidden
  });
});

<div id="showHideInfoXmas" style="display: block; background: rgba(0,0,0,0.15); clear: both; width: 100%; padding: 40px 20px; margin: 0 auto; text-align: center; max-width: 1000px; position: relative;">
  <div id="showHideInfoXmasButton" style="background: white; position: absolute; top: 10px; right: 10px; padding: 10px; color: red;">X</div>
  <h2 style="color: red">IMPORTANT NEWS - Christmas closing, the weather and postage times</h2>
  <p><b>Christmas opening times:</b> on Wednesday, December the 20th at 11am we are closed for our winter break, and re-opening on Wednesday the 3rd January 2018. Any orders placed after 9AM on December the 20th will not be processed until January 3rd at
    the earliest.</p>
  <p><b>Snowfall:</b> Due to the adverse weather conditions and the fact that it is our busiest time of the year, please be aware that any orders placed may take longer than usual to be dispatched. If your order is time-sensitive, please call us on 01295
    788522.
  </p>
  <p><b>Stock levels:</b> Please note: as it is the busiest time of the year for us, our stock levels will be unpredictable, to avoid disappointment please get your orders in as soon as possible!</p>
  <p><b>Postage:</b> As with any other online retailer, this time of the year sees extraordinary stresses on the national and world-wide postage and courrier network and your order may be subject to delays beyond of our control.</p>
  <p>Rest assured; we are working very hard to ensure your orders are despatched as quickly as possible. Thank you for your understanding!</p>
</div>

1 个答案:

答案 0 :(得分:0)

事实是,我不知道为什么它不起作用,但这里的代码对我有用:

  <script type="text/javascript">
jQuery(document).ready(function($){

  if (typeof Cookies('divhideXMAS') === 'undefined'){
   //no cookie
    jQuery('#showHideInfoXmas').show();
  } else {
  }
  jQuery('#showHideInfoXmasButton').click(function() {
  jQuery(this).parent().hide();
    Cookies.set('divhideXMAS', true);
  });     
 });

然后我默认使用css隐藏div。