【问题标题】:Why is my "show/hide with cookie" jquery code not working?为什么我的“使用 cookie 显示/隐藏”jquery 代码不起作用?
【发布时间】:2018-05-27 04:11:10
【问题描述】:

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

它不工作!

我想让用户关闭消息并让网站记住他们的选择。

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

$(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>

【问题讨论】:

  • 鉴于您网站上的 $ is not a function 错误,您似乎没有包含 jQuery。另请注意,所有相关代码都应放在问题中
  • 我会继续尝试保存您的问题。请记住 Rory 上述关于在您的问题中包含所有相关代码的观点​​。
  • 抱歉,Rory,谢谢尤达大师。下次将包括完整的代码。代码不会格式化,因此使用了要点。我确实包含了 jquery,所以不知道你为什么会看到这个错误,Rory?
  • @tjcss 当你说它不工作时,什么不完全工作? cookie不保存还是JS抛出错误?
  • 将javascript部分包裹在$(document).ready(function(){ // here })中,看看它是否工作。

标签: jquery cookies


【解决方案1】:

说实话,我真的不知道为什么它不起作用,但下面是对我有用的代码:

  <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。

【讨论】:

    猜你喜欢
    • 2018-04-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-04-15
    • 1970-01-01
    • 2013-08-14
    • 2011-06-29
    • 1970-01-01
    相关资源
    最近更新 更多