我有一個測試網站here(仍在開發中),並且我試圖在頂部點擊關閉時隱藏頂部的小通知。如何獲得JavaScript Cookie來記住DIV狀態?
目前我的劇本是這樣的:
<style type="text/css">
<!--
.hide {
display:none;
}
.show {
display:block;
}
-->
</style>
<script type="text/javascript">
<!--
var state;
window.onload=function() {
obj=document.getElementById('alert');
state=(state==null)?'show':state;
obj.className=state;
document.getElementById('close').onclick=function() {
obj.className=(obj.className=='show')?'hide':'show';
state=obj.className;
setCookie();
return false;
}
}
function setCookie() {
exp=new Date();
plusMonth=exp.getTime()+(31*24*60*60*1000);
exp.setTime(plusMonth);
document.cookie='State='+state+';expires='+exp.toGMTString();
}
function readCookie() {
if(document.cookie) {
state=document.cookie.split('State=')[1];
}
}
readCookie();
//-->
</script>
然後:
<div id="alert" class="show" style="float: left; width: 100%;"><div style="width: 80%; text-align:left; float:left;">Sorry for the downtime, we were experiencing some problems with our web host. Everything should be back to normal now =D</div>
<div style="width: 18%; text-align:right; float:left; padding-right: 20px;"><a href='#' id="close">close</a></div> </div>
,但它似乎並沒有工作。有任何想法嗎?
在此先感謝!
山姆
完美的作品 - 非常感謝! – 2009-08-16 12:31:57
@sm - 呃,我的答案是否解決您的問題? – karim79 2009-08-16 12:42:37
噢,對不起 - 以爲Mage先生先回答了。固定=) – 2009-08-16 12:47:36