用Cookie来保存菜单当前位置代码
代码简介:用JavaScript操作Cookie实现菜单保存的一个实例,如果你正在编写一个JS层拖动的功能,那么这段代码可以帮你实现拖动位置的保存,让浏览器去保存用户拖动网页某个层的位置,这样是不是方便了很多?
代码内容:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1- transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/html; charset=gb2312" /> <title>用Cookie来保存菜单当前位置代码_网页代码站(www.webdm.cn)</title> <style> a{color:#5A73F3;text-decoration:none} body{background:#003399;margin:28px;font-size:10.5pt;} #menu li{float:left;height:26px;line-height:26px;list-style:none} #menu li a{padding:10px;} a:hover{background:skyblue;color:#fff} a.hover{height:26px;line-height:26px;background:red;color:#fff} a,area{blr:e-xpression(this.onFocus=this.blur())} :focus{-moz-outline-style:none;} </style> </head> <body> <font color=yellow>点击一下菜单,你可以刷新一下页面,是不是选中的菜单还停留在刚才的位置? <div id="menu"> <ul> <li><a href="javascript:void(0)" class="hover" onclick="changename(0)" hidefocus="true">WebDm.Cn</a></li> <li><a href="javascript:void(0)" onclick="changename(1)" hidefocus="true">ASP源码</a></li> <li><a href="javascript:void(0)" onclick="changename(2)" hidefocus="true">PHP源码</a></li> <li><a href="javascript:void(0)" onclick="changename(3)" hidefocus="true">JSP源码</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; } // 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) </script> </body> </html> <br> <p><a href="http://www.webdm.cn">网页代码站</a> - 最专业的代码下载网站 - 致力为中国站长提供有质量的代码!</p>
代码来自:http://www.webdm.cn/webcode/eb36b1d7-5f94-44f6-bb1a-f6beef3f73f5.html