如题,我在网上找了个带记忆功能的导航菜单代码,地址http://www.codefans.net/jscss/code/1257.shtml,开始很开心,后来发现了个问题,这个只能在一个页面上,可实际情况是导航菜单肯定是连接到不同的页面的,这就不好使了
(我将它写在了一个自定义控件里面,不知道为什么在点了之后页面是跳转到新的页面了,但是导航菜单的背景没有跟随变动)
代码给出,请高手指点,或者 能给我个 具体的代码
样式:
<style>
a{color:#5A73F3;text-decoration:none}
body{background:#333;margin:30px;font-size:14px;}
#menu li{float:left;height:25px;line-height:25px;list-style:none}
#menu li a{padding:10px;}
a:hover{background:#ccc;color:##3399FF}
a.hover{height:25px;line-height:25px;background:red;color:#ffffff}
a,area{blr:e-xpression(this.onFocus=this.blur())}
:focus{-moz-outline-style:none;}
</style>内容:
<div id="menu">
<ul>
<li><a href="javascript:void(0)" class="hover" onclick="changename(0)" hidefocus="true">我的菜单</a></li>
<li><a href="javascript:void(0)" onclick="changename(1)" hidefocus="true">网页模板</a></li>
<li><a href="javascript:void(0)" onclick="changename(2)" hidefocus="true">精品代码</a></li>
<li><a href="javascript:void(0)" onclick="changename(3)" hidefocus="true">设计素材</a></li>
<li><a href="javascript:clear();" hidefocus="true">恢复初始</a></li>
</ul>
</div>
<script language="javascript">
function changename(c,cl)
{
var d=document.getElementById("menu").getElementsByTagName("a");
if(!cl)
{
writeCookie("hovers",c,222);
}
c=readCookie("hovers")?readCookie("hovers"):c;
for(i=0;i<d.length;i++)
{
d[i].className=i==c?"hover":"";
}
}
function writeCookie(name, value, hours)
{
var expire = "";
if(hours != null)
{
expire = new Date((new Date()).getTime() + hours * 3600000);
expire = "; expires=" + expire.toGMTString();
}
document.cookie = name + "=" + escape(value) + expire;
}
// Example:
// alert( readCookie("myCookie") );
function readCookie(name)
{
var cookieValue = "";
var search = name + "=";
if(document.cookie.length > 0)
{
offset = document.cookie.indexOf(search);
if (offset != -1)
{
offset += search.length;
end = document.cookie.indexOf(";", offset);
if (end == -1) end = document.cookie.length;
cookieValue = unescape(document.cookie.substring(offset, end))
}
}
return cookieValue;
}
function clear()
{
writeCookie("hovers","",222);
document.location=document.location.href;
}
changename(0,1)
(我将它写在了一个自定义控件里面,不知道为什么在点了之后页面是跳转到新的页面了,但是导航菜单的背景没有跟随变动)
代码给出,请高手指点,或者 能给我个 具体的代码
样式:
<style>
a{color:#5A73F3;text-decoration:none}
body{background:#333;margin:30px;font-size:14px;}
#menu li{float:left;height:25px;line-height:25px;list-style:none}
#menu li a{padding:10px;}
a:hover{background:#ccc;color:##3399FF}
a.hover{height:25px;line-height:25px;background:red;color:#ffffff}
a,area{blr:e-xpression(this.onFocus=this.blur())}
:focus{-moz-outline-style:none;}
</style>内容:
<div id="menu">
<ul>
<li><a href="javascript:void(0)" class="hover" onclick="changename(0)" hidefocus="true">我的菜单</a></li>
<li><a href="javascript:void(0)" onclick="changename(1)" hidefocus="true">网页模板</a></li>
<li><a href="javascript:void(0)" onclick="changename(2)" hidefocus="true">精品代码</a></li>
<li><a href="javascript:void(0)" onclick="changename(3)" hidefocus="true">设计素材</a></li>
<li><a href="javascript:clear();" hidefocus="true">恢复初始</a></li>
</ul>
</div>
<script language="javascript">
function changename(c,cl)
{
var d=document.getElementById("menu").getElementsByTagName("a");
if(!cl)
{
writeCookie("hovers",c,222);
}
c=readCookie("hovers")?readCookie("hovers"):c;
for(i=0;i<d.length;i++)
{
d[i].className=i==c?"hover":"";
}
}
function writeCookie(name, value, hours)
{
var expire = "";
if(hours != null)
{
expire = new Date((new Date()).getTime() + hours * 3600000);
expire = "; expires=" + expire.toGMTString();
}
document.cookie = name + "=" + escape(value) + expire;
}
// Example:
// alert( readCookie("myCookie") );
function readCookie(name)
{
var cookieValue = "";
var search = name + "=";
if(document.cookie.length > 0)
{
offset = document.cookie.indexOf(search);
if (offset != -1)
{
offset += search.length;
end = document.cookie.indexOf(";", offset);
if (end == -1) end = document.cookie.length;
cookieValue = unescape(document.cookie.substring(offset, end))
}
}
return cookieValue;
}
function clear()
{
writeCookie("hovers","",222);
document.location=document.location.href;
}
changename(0,1)
可直接运行演示...
http://www.jb51.net/article/10516.htm
首先谢谢你给我的帮助,不过你给的这个和我原来的是一样的,就是只能在当前页防止刷新,如果像我说的那样在不同页面调用同一个自定义控件(包含这个代码)的时候,不能准确记忆
另外这个代码在firefox下是有问题的,你的浏览器是什么?