Cookie记住菜单状态的问题

thw19850316 2009-05-14 02:34:35
<head>
<style type="text/css">
BODY { FONT-SIZE: 12px;}
TD { FONT-SIZE: 12px; line-height: 150%}
.resizeDivClass
{
position:relative;
background-color:red;
width:2;
z-index:1;
left:expression(this.parentElement.offsetWidth-1);
cursor:e-resize;

}
a:link,a:visited {color:#385065;text-decoration:none}
a:hover {text-decoration:underline; color:#6699cc}
a:active{color:Red;text-decoration:none}
#menu {width:100%;margin:0px 0px;padding:0px;text-align:left;list-style:none; height:370px}
#menu.item:hover { COLOR:white; font-weight:bold; FONT-SIZE: 12px; TEXT-DECORATION: none; background-color:olivedrab; display:block; width:100%; line-height:12pt;}
#menu .item {margin:0px 0px;padding:0px;list-style:none}
a.title:link, a.title:visited, a.title:hover {display:block;color:Black;font-weight:bold;padding:0px 0 0 0px;width:100%;line-height:23px;cursor:pointer;text-decoration:none;}
#menu .item ul {border:1px solid #9FACB7;margin:0;width:100%;padding:0px 0px 0px 0px;background:#fff;list-style:none;display:none;height:370px}
#menu .item ul li {display:block;}
#menu .item ul li a:hover {Color:white;font-weight:bold; FONT-SIZE: 12px; TEXT-DECORATION: none; background-color:olivedrab; display:block; width:100%; line-height:12pt;}

</style>

<script language="javascript" type="text/javascript">
// --- 获取ClassName
document.getElementsByClassName = function(cl) {
var retnode = [];
var myclass = new RegExp('\\b'+cl+'\\b');
var elem = this.getElementsByTagName('*');
for (var j = 0; j < elem.length; j++) {
var classes = elem[j].className;
if (myclass.test(classes)) retnode.push(elem[j]);
}
return retnode;
}

// --- 隐藏所有
function HideAll() {
var items = document.getElementsByClassName("optiton");
for (var j=0; j<items.length; j++) {
items[j].style.display = "none";
}
}

// --- 设置cookie
function setCookie(sName,sValue,expireHours) {
var cookieString = sName + "=" + escape(sValue);
//;判断是否设置过期时间
if (expireHours>0) {
var date = new Date();
date.setTime(date.getTime + expireHours * 3600 * 1000);
cookieString = cookieString + "; expire=" + date.toGMTString();
}
document.cookie = cookieString;
}

//--- 获取cookie
function getCookie(sName) {
var aCookie = document.cookie.split("; ");
for (var j=0; j < aCookie.length; j++){
var aCrumb = aCookie[j].split("=");
if (escape(sName) == aCrumb[0])
return unescape(aCrumb[1]);
}
return null;
}

window.onload = function() {
var show_item = "opt_1";
if (getCookie("show_item") != null) {
show_item= "opt_" + getCookie("show_item");
}
document.getElementById(show_item).style.display = "block";
var items = document.getElementsByClassName("title");
for (var j=0; j<items.length; j++) {
items[j].onclick = function() {
var o = document.getElementById("opt_" + this.name);
if (o.style.display != "block")
{
HideAll();
o.style.display = "block";
setCookie("show_item",this.name);
}
else {
o.style.display = "none";
}
}
}
}
</script>
</head>
<body>
<div id="divMenu" style="height:400px" runat="server">
<ul id="menu">
<li class="item" style="background-image:url(../images/menubar.gif); text-align:center; margin:1px 1px 1px 1px"><a href="#" class="title" name="1" style="width: 100%; text-align:center">管理</a>
<ul id="opt_1" class="optiton">
<li><a href="../Folder1/WForm1.aspx">收费</a></li>
<li><a href="../Folder1/WForm2.aspx">信息录入</a></li>
<li><a href="../Folder1/WForm3.aspx">记录查询</a></li>
</ul>
</li>
<li class="item" style="background-image:url(../images/menubar.gif); text-align:center; margin:1px 1px 1px 1px"><a href="#" class="title" name="2" style="width: 100%">基础数据管理</a>
<ul id="opt_2" class="optiton">
<li><a href="../Folder2/MForm1.aspx">基本管理</a></li>
<li><a href="../Folder2/MForm2.aspx">单位管理</a></li>
<li><a href="../Folder2/MForm3.aspx">标准管理</a></li>
</ul>
</li>

<li class="item" style="background-image:url(../images/menubar.gif); text-align:center; margin:1px 1px 1px 1px"><a href="#" class="title" name="3" style="width: 100%">系统帮助</a>
<ul id="opt_3" class="optiton">
<li><a href="../Folder3/Form1.aspx">帮助主题</a></li>
<li><a href="../Folder3/Form2.aspx">关于系统</a></li>
<li><a href="../Folder3/Form3.aspx">安全退出</a></li>
</ul>
</li>
</ul>
</div>
</body>
...全文
161 12 打赏 收藏 转发到动态 举报
写回复
用AI写文章
12 条回复
切换为时间正序
请发表友善的回复…
发表回复
thw19850316 2009-06-05
  • 打赏
  • 举报
回复
换另一种思路了,谢谢各位!
中年秃头大叔 2009-05-15
  • 打赏
  • 举报
回复
顶一下.
qepwqlpf 2009-05-15
  • 打赏
  • 举报
回复
sdf
qepwqlpf 2009-05-15
  • 打赏
  • 举报
回复
来学习了
DemonAngle 2009-05-14
  • 打赏
  • 举报
回复
继续帮忙顶
thw19850316 2009-05-14
  • 打赏
  • 举报
回复
回复:bizbuy

但是我设置了js里的路径还是不行,我在这里cookieString = cookieString + "; expire=" + date.toGMTString() + "; path=/";
搞不清到底为什么了
jlj84237485 2009-05-14
  • 打赏
  • 举报
回复
帮顶
bzhyan 2009-05-14
  • 打赏
  • 举报
回复
帮搂主顶
bizbuy 2009-05-14
  • 打赏
  • 举报
回复
[Quote=引用 1 楼 thw19850316 的回复:]
这是母版页的导航菜单,母版页在Main文件夹下,为什么这个导航菜单可以记住上次的(因为使用cookie记住了选择),但是要点两次才返回现在的(可能js点击的时候没有获取cookie以更新),如果所有的内容页不在文件夹下,而直接在项目目录下没有问题(看看路径是否有问题)
[/Quote]
thw19850316 2009-05-14
  • 打赏
  • 举报
回复
如果这些链接是这样的,没有问题:
<li> <a href="WForm1.aspx">收费 </a> </li>
<li> <a href="WForm2.aspx">信息录入 </a> </li>
<li> <a href="WForm3.aspx">记录查询 </a> </li>
也就是内容也不在文件夹下,而直接在解决方案目录下
thw19850316 2009-05-14
  • 打赏
  • 举报
回复
这是母版页的导航菜单,母版页在Main文件夹下,为什么这个导航菜单可以记住上次的,但是要点两次才返回现在的,如果所有的内容页不在文件夹下,而直接在项目目录下没有问题

62,046

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术交流专区
javascript云原生 企业社区
社区管理员
  • ASP.NET
  • .Net开发者社区
  • R小R
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

.NET 社区是一个围绕开源 .NET 的开放、热情、创新、包容的技术社区。社区致力于为广大 .NET 爱好者提供一个良好的知识共享、协同互助的 .NET 技术交流环境。我们尊重不同意见,支持健康理性的辩论和互动,反对歧视和攻击。

希望和大家一起共同营造一个活跃、友好的社区氛围。

试试用AI创作助手写篇文章吧