问题
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