1

如果单击 X,我有一个跨度的促销标题。现在,当我刷新页面时,跨度又回来了。我需要 cookie 来记住跨度关闭的 24 小时。

这是我尝试但没有结果的方法:

html代码:

<div id=”span” class="promo_holder">
    <a href="javascript:void(0)" id=”close” class="close_promo_header" 
       onclick="hidePromoPanel ('/');">Close<span>X</span></a>
</div>

JavaScript 代码:

$(document).ready(function() {


    if (!readCookie('hide')) {
        $('#span').show();
    }


    $('#close').click(function() {
        $('#span').hide();
        createCookie('hide', true, 1)
        return false;
    });

});


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

0 回答 0