[英]Saving JQuery Toggle State with Cookie
我有一个iFrame,可在单击按钮时将其切换为可见或隐藏。 我正在使用JQuery .toggle函数来执行此操作。 当前,默认情况下通过CSS隐藏该区域的div:style =“ display:none”
提出了一项新要求,要求为下一次页面加载保存用户最近的显示或隐藏(选定的切换状态),因此,如果他们选择在上一次访问主页时显示iFrame,则下次访问他们访问过的网站默认情况下是可见的。 我想使用Cookie设置最后选择的切换状态,但无法正常工作。 下面是我的代码:
<meta charset="utf-8">
<title>drop demo</title>
<link rel="stylesheet" href="//code.jquery.com/ui/1.10.4/themes/smoothness/jquery-ui.css">
<style>
#toggle {
width: 100px;
height: 100px;
background: #ccc;
}
.button {
padding-top: 10px;
padding-right: 20px;
}
</style>
<script src="//code.jquery.com/jquery-1.10.2.js"></script>
<script src="//code.jquery.com/ui/1.10.4/jquery-ui.js"></script>
<button type="button" id="button"
title="Personalized Sales Dashboard">Show/Hide</button>
<div id="toggle" style="display:none" frameborder="2">
<iframe src="https://sample.com"="" id="myfr" scrolling="no" frameborder="0"
height="800px" width="100%">Your Browser Do not Support Iframe</iframe>
</div>
<script>
$("#button").click(function () {
$("#toggle").toggle("blind");
});
$(function () {
$("#button").tooltip();
});
</script>
在按钮上设置一个cookie,单击div是否可见:
document.cookie = "toggleState=" + $("#toggle").is(':visible');
并在页面加载时检索它,相应地显示或隐藏内容:
var toggleState = document.cookie.replace(/(?:(?:^|.*;\s*)toggleState\s*\=\s*([^;]*).*$)|^.*$/, "$1");
if (toggleState == 'true') {
$("#toggle").show();
}
else {
$("#toggle").hide();
}
https://developer.mozilla.org/en-US/docs/Web/API/document.cookie
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.