Set cookies with Javascript to remember show state by click

喜夏-厌秋 提交于 2020-01-03 05:34:05

问题


Please, advice. I want to set cookies to my working simple javascript code. The div is showed by clicking on the link, but if the page reloads the div will hide. I need to set cookies to remember the show state for 7 days for e.g.

Javascript

function showDiv() {
document.getElementById('auth_block').style.display = "block";
}

HTML

<a href="#" onclick="showDiv()">
      <img src="/images/cont_btn.png">
    </a>

<div style="display:none;" id="auth_block">Some information</div>

回答1:


With jQuery, you can use $.cookie():

$('a').click(function() {
    if($.cookie('display') != null) {
        $('#auth_block').show();
    } else {
        $.cookie('display', $('#auth_block').css('display'), { expires: 7 });
        $('#auth_block').show();
    }
});


来源:https://stackoverflow.com/questions/22494851/set-cookies-with-javascript-to-remember-show-state-by-click

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!