隐藏div 24小时javascript javascript? [英] Hide div 24hr cookie javascript?
问题描述
我想要一个简单的JavaScript代码,允许我在点击预定义的时间时隐藏某个div元素。为了更多信息,我有一个建议框,当主页加载时出现。我想要的是当div关闭按钮被点击时,它设置一个cookie来保持box div 24小时(1天)。简单地说,当按下div关闭按钮时,box div隐藏了24小时。注意:我有一个javascript允许关闭按钮关闭框,但它会加载每次刷新。
I would like a simple JavaScript code that will allow me to hide a certain div element when clicked for a predefined amount of time. To be a little more informative, I have a suggestions box that appears when the home page is loaded. What I would like is when the div close button is clicked it sets a cookie to keep the box div closed for 24 hours (1day). Simply said, when the div close button is pressed, the box div is hidden for 24 hours. Note: I have a javascript that allows the close button to close the box but it will load every refresh.
http://i.stack.imgur.com/du1pA.jpg
推荐答案
虽然TJ Crowder在他的评论是正确的stackoverflow不在这里写你的代码...我为你写了一些代码。这里是一个使用jQuery的解决方案。在其中,您可以为邮件使用< div id =popupDiv> ...< / div>
关闭div。
Although T.J. Crowder is right in his comment that stackoverflow is not here for writing your code... I wrote some code for you. Here's a solution using jQuery. In it you'd use a <div id="popupDiv">...</div>
for the message and a link in it with id "close" to close the div.
$(document).ready(function() {
// If the 'hide cookie is not set we show the message
if (!readCookie('hide')) {
$('#popupDiv').show();
}
// Add the event that closes the popup and sets the cookie that tells us to
// not show it again until one day has passed.
$('#close').click(function() {
$('#popupDiv').hide();
createCookie('hide', true, 1)
return false;
});
});
// ---
// And some generic cookie logic
// ---
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);
}
这里是一个js小提琴: http://jsfiddle.net/FcFW2/1/ 。运行一次,然后再次运行。第二次弹出窗口不显示。
Here's a js fiddle: http://jsfiddle.net/FcFW2/1/. Run once and then run again. The second time the popup does not show.
这篇关于隐藏div 24小时javascript javascript?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!