如何保持显示/隐藏侧边栏的设置
我制作了一个侧边栏,它旨在始终显示,只有当单击它时才会隐藏。但是隐藏此内容后,如果我刷新页面,它会再次显示。所以我想保留设置,这样如果用户隐藏它,它就不会出现,直到他按下显示按钮,无论页面刷新还是转到另一个页面都无关紧要。
(例如,像 facebook 右侧聊天栏一样,它保持隐藏或显示,与页面刷新或其他任何事情无关)
我想 JS 上的一些更改可以完成这项工作,但我不确定&不是编码员,所以请帮我做。
我使用的 JS 代码:
<script language=javascript type='text/javascript'>
function hideDiv() {
if (document.getElementById) { // DOM3 = IE5, NS6
document.getElementById('hideShow').style.visibility = 'hidden';
} else {
if (document.layers) { // Netscape 4
document.hideShow.visibility = 'hidden';
} else { // IE 4
document.all.hideShow.style.visibility = 'hidden';
}
}
}
function showDiv() {
if (document.getElementById) { // DOM3 = IE5, NS6
document.getElementById('hideShow').style.visibility = 'visible';
} else {
if (document.layers) { // Netscape 4
document.hideShow.visibility = 'visible';
} else { // IE 4
document.all.hideShow.style.visibility = 'visible';
}
}
}
</script>
HTML 和 CSS
<div id="hideShow" ..etc>
My content
</div>
以及调用 JavaScript 来隐藏它:
<a href="javascript:hideDiv()">Hide Div</a>
以及显示它:
<a href="javascript:showDiv()">show Div</a>
I made a sidebar it is meant to show all time, only when it is clicked it will hide. But after hiding this if I refresh the page it get shown again. So I want to keep settings so if user hide this it wont appear until he press the show button doesnt matter the page refresh or he goto another page.
(For example like facebook right chatbar which stays hidden or shown doesnt matter the page refresh or anything)
I guess some changes on JS will do the job but im not sure & not a coder so help me do it.
the JS code I used:
<script language=javascript type='text/javascript'>
function hideDiv() {
if (document.getElementById) { // DOM3 = IE5, NS6
document.getElementById('hideShow').style.visibility = 'hidden';
} else {
if (document.layers) { // Netscape 4
document.hideShow.visibility = 'hidden';
} else { // IE 4
document.all.hideShow.style.visibility = 'hidden';
}
}
}
function showDiv() {
if (document.getElementById) { // DOM3 = IE5, NS6
document.getElementById('hideShow').style.visibility = 'visible';
} else {
if (document.layers) { // Netscape 4
document.hideShow.visibility = 'visible';
} else { // IE 4
document.all.hideShow.style.visibility = 'visible';
}
}
}
</script>
HTML and CSS
<div id="hideShow" ..etc>
My content
</div>
and this to call the JavaScript to hide it:
<a href="javascript:hideDiv()">Hide Div</a>
and this to show it:
<a href="javascript:showDiv()">show Div</a>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
像这样的东西!这里有一个读取cookie的函数,因为设置cookie很容易,但读取的次数不多。
祝你好运
Something like this! here you have a function to read the cookie, because set a cookie is easy, but read it not to much.
good luck
您需要一个 javascript 类来读取和写入 cookie。这样,当用户与侧边栏交互时,您可以将侧边栏的状态写入 cookie。当页面加载时,您从 cookie 中读取状态,并相应地设置可见性。
You need a javascript class to read and write cookies. This way, you can write the state of the sidebar in the cookie when user interacts with it. When the page loads, you read the state from the cookie, and set visibility accordingly.
您需要将栏的可见性状态保存在某个变量中。由于您仅使用(我假设)JS,因此您可能需要访问用户的cookie。
如何在JS中设置Cookie: http://www.javascripter.net/faq/settinga.htm
如何在JS中读取Cookie: http://www.javascripter.net/faq/readinga.htm
You'll need to save the state of the bar's visibility in a variable somewhere. Since you are using (I assume) only JS, you'll probably need to access the user's cookies.
How to Set a Cookie in JS: http://www.javascripter.net/faq/settinga.htm
How to Read a Cookie in JS: http://www.javascripter.net/faq/readinga.htm