如果这个cookie!== null然后执行此操作

时间:2014-07-17 21:25:05

标签: javascript jquery cookies jquery-cookie

carhartl的插件jquery-cookie存在一些问题...只是出现了一个粘贴脚本... ...

这个想法是,如果另外关闭,有一个始终位于页面底部的stickfooter。很简单。

因此,十字架的onclick会触发两件事:隐藏stickfooter div并添加会话cookie。

现在,它会瞬间弹出,然后无限期地隐藏。但是还没有设置cookie。

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

    if (jQuery.cookie('stickyNewsClosed') === null) {
        jQuery('.stickyFooter').show();
    }
        if (jQuery.cookie('stickyNewsClosed') !== null) {
            jQuery('.stickyFooter').hide();
        }
});
</script>

<script type="text/javascript">
function closeSticky(){
    jQuery('.stickyFooter').hide();
    jQuery.cookie('stickyNewsClosed', 'yup', {
                path: '/'
            });
}
</script>

也许使用.css()jquery方法而不是.hide()/ .show()?

1 个答案:

答案 0 :(得分:1)

嗨,请看这里:http://plnkr.co/edit/yaXgcEsMuNaGu5dQJnL0?p=preview

jQuery(document).ready(function() {
console.log(jQuery.cookie('stickyNewsClosed'));
  if (jQuery.cookie('stickyNewsClosed') === undefined) {
    jQuery('.stickyFooter').show();
  }
 else if (jQuery.cookie('stickyNewsClosed') !== null) {
    jQuery('.stickyFooter').hide();
  }
});



function closeSticky() {

  jQuery('.stickyFooter').hide();
  jQuery.cookie('stickyNewsClosed', 'yup', {
    path: '/'
  });
}