【问题标题】:div hidden by cookie reopens in IE only被 cookie 隐藏的 div 仅在 IE 中重新打开
【发布时间】:2015-05-06 18:28:04
【问题描述】:

javascript 不是我的强项,因此我们将不胜感激任何正确方向的帮助或帮助!

基本上我有一个隐藏在我的页面上的 div,只有当他们是根据 cookie 设置的新访问者时才会显示给用户,但是它似乎在 firefox 和 chrome 中都运行良好,但在单击时使用 IE关闭按钮页面跳回顶部并显示隐藏的div(不应该,它应该保持隐藏)

HTML

<div id="theLink">
<?php if($this->countModules('tekenin2')) : ?>
<div id="gototop">
  <div id="popup"><a href="#" onclick="parentNode.remove();return false; "><img src="/templates/marktoe/images/close.png" id="close" class="close" border="0" alt="close" /></a>
    <jdoc:include type="modules" name="tekenin2" style="xhtml" />
  </div>
</div>
<?php endif; ?>

脚本 1

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 < 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);
}

function setTheDivStyle() { // body on load event
    if (!readCookie('visited')) {

        // if cookie not found display the div and create the cookie
        document.getElementById("theLink").style.display = "block";
        createCookie('visited', 'visited', 1); // 1 day = 24 hours persistence
    } else {
        // if cookie found hide the div
        document.getElementById("theLink").style.display = "none";
    }
}

脚本 2 - 我怀疑问题出在哪里,但我可以确定

window.addEvent('domready',function() {
    /* smooth */
    new SmoothScroll({duration:500});
    /* link management */
    $('theLink').set('opacity','0').setStyle('display','block');

    /* scrollspy instance */
    var ss = new ScrollSpy({
        min: 10,
        onEnter: function(position,enters) {
            //if(console) { console.log('Entered [' + enters + '] at: ' + position.x + ' / ' + position.y); }
            $('theLink').fade('in');
        },
        onLeave: function(position,leaves) {
            //if(console) { console.log('Left [' + leaves + '] at: ' + position.x + ' / ' + position.y); }
            //value below was #gototop div
            $('theLink').fade('out');
        },
        onTick: function(position,state,enters,leaves) {
            //if(console) { console.log('Tick  [' + enters + ', ' + leaves + '] at: ' + position.x + ' / ' + position.y); }
        },
        container: window
    });

});

我需要的是要隐藏的 div,使用 cookie 检查用户是新用户还是返回用户,如果新则显示 div 否则隐藏它。

任何提示或建议将不胜感激,就像我说这不是我的强项,我觉得我缺少一些基本的东西。我浏览了多个其他线程,试图绕开它,但无济于事。 提前致谢!

【问题讨论】:

  • 此外,当页面加载时,有时 div 会快速显示(在被脚本隐藏之前闪烁),但我不知道为什么。
  • 关于闪现问题:您可以尝试默认隐藏它,仅在cookie不存在时才显示它。
  • 确实,感谢您的回复,但是第一个sn-p中的最后一段代码不应该解决这个问题吗?

标签: javascript jquery css cookies jquery-cookie


【解决方案1】:

在你的 HTML 中,替换

parentNode.remove();

this.parentNode.parentNode.removeChild(this.parentNode);

如果你想移除整个 div,你可能不得不上去(添加更多 .parentNode)——当前代码移除了弹出 div。

【讨论】:

  • 非常感谢 - 好的,这有助于了解删除 div。我实现了您建议的版本,但是如果 div 最初弹出,我单击关闭,然后它再次弹出。只有当我导航到另一个页面时它才会消失(我猜那是在设置 cookie 时)但是在我单击关闭按钮而不导航到另一个页面后是否无法强制它保持关闭状态?
  • 我猜这是因为 a 元素的缘故。尝试将“a”标签(关闭按钮的)更改为跨度。
  • 如果我将其更改为 span,则“a”元素(关闭按钮)将停止运行 :(
  • 很抱歉 - 它应该是一个 div。 span 里面不能有 img 。或者可能是一个按钮元素。
  • 没问题,呵呵,谢谢 - 所以你说的是这样的? &lt;div href="#" onclick="this.parentNode.removeChild(this.parentNode.this.parentNode);return false; "&gt;&lt;img src="/templates/marktoe/images/close.png" id="close" class="close" border="0" alt="close" /&gt;&lt;/div&gt; 感谢您抽出宝贵时间查看 :) 但链接仍然无法正常工作。对不起,我一定错过了什么..
猜你喜欢
  • 2010-11-18
  • 2013-12-05
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2023-03-13
相关资源
最近更新 更多