您可以通过每次状态更改设置cookie的值,并检查该cookie的值在页面加载时设置的初始状态相匹配的预先设定的cookie值不喜欢这样写道:
添加这个CSS通过封闭来使默认状态,所以如果需要的最终状态是封闭的,它最初不会显示为打开,然后关闭:
.toggled_content {display: none;}
添加此javascript:
<script type="text/javascript">
$(document).ready(function(){
function hideContent() {
$(".toggled_content").slideUp();
$("img.upmid").attr('src',"images/downmid.png");
}
function showContent() {
$(".toggled_content").slideDown();
$("img.upmid").attr('src',"images/upmid.png");
}
// set initial state based on cookie value
// assumes default state is closed
if (readCookie("updown") == "down") {
showContent();
}
$(".toggle_parent").toggle(function(){
hideContent();
createCookie("updown", "up", 30)
},function(){
showContent();
createCookie("updown", "down", 30);
});
});
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);
}
</script>
辉煌,曾立竿见影。仅仅为了美观,会有更好的方法来保持div在加载时关闭。目前,如果切换关闭,其负载打开,然后滑回? – user1558300 2012-07-27 17:29:22
@ user1558300 - 我修改了我的答案,以便初始状态关闭,因此如果所需的最终状态关闭,它将保持关闭状态。如果所需的最终状态是打开的,它将在页面加载时打开。 – jfriend00 2012-07-27 17:39:15