树形菜单,刷新后不变

Javascript语句:

<script language="JavaScript" type="text/javascript">

    //收缩或展开树形菜单并写入Cookie

    function Show(id) {

        var obj = document.getElementById('div' + id);

        if (obj.style.display == "none") {

            obj.style.display = "";

            //将子菜单Id放入Cookies

            var curShow = readCookie('curShow');

            if (curShow != '') {

                var arr_curShow = curShow.split(',');

                var found = false;

                for (i = 0; i < arr_curShow.length - 1; i++) {

                    if (arr_curShow[i].toString() == id) {

                        found = true;

                    }

                }

                if (!found) {

                    writeCookie('curShow', curShow + ',' + id, 12);

                }

            }

            else {

                writeCookie('curShow', id, 12);

            }

            //将子菜单Id放入Cookies End

        } else {

            obj.style.display = "none";

            //将子菜单Id从Cookies中移除

            var curShow = readCookie('curShow');

            if (curShow != '') {

                var arr_curShow = curShow.split(',');

                for (i = 0; i < arr_curShow.length; i++) {

                    if (arr_curShow[i].toString() == id) { arr_curShow = arr_curShow.del(i--); }

                }

                curShow = arr_curShow.join(',');

                writeCookie('curShow', curShow, 12)

            }

            //将子菜单Id从Cookies中移除 End

        }

 

    }

 

    //读取Cookie

    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;

    }

 

    //写入Cookie

    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;

    }

 

    Array.prototype.del = function(n) {  //n表示第几项,从0开始算起。

        //prototype为对象原型,注意这里为对象增加自定义方法的方法。

        if (n < 0) {  //如果n<0,则不进行任何操作。

            return this;

        }

        else {

            return this.slice(0, n).concat(this.slice(n + 1, this.length));

        }

    }

 

    //初始化树形菜单

    function a() {

        var curShow = readCookie('curShow');

        var totalShow = 7;  //自己修改一共有多少个二级分类

        if (curShow != '' && curShow != null) {

            for (i = 1; i <= totalShow; i++) {

                document.getElementById('div' + i).display = "none";

            }

            var arr_curShow;

            arr_curShow = curShow.split(',');

            for (i = 0; i <= arr_curShow.length - 1; i++) {

                if (arr_curShow[i] != '') {

                    document.getElementById('div' + arr_curShow[i]).style.display = "";

                }

            }

        }

        play();

    }

</script>

Body内:

<h4><span><a href="zyyw_ztjs.aspx?moduleid=937">一级菜单</a></span></h4>

    <h5 οnclick="Show(1)"><span>二级菜单</span></h5>

        <div id="div1" style="display: none;">

        </div>

    <h5 οnclick="Show(2)"><span>二级菜单</span></h5>

        <div id="div2" style="display: none;">

        </div>

    <h5 οnclick="Show(3)"><span>二级菜单</span></h5>

        <div id="div3" style="display: none;">

            <h5 style=" font-size: 9pt; padding-left: 22px;"><span>三级菜单</span></h5>

                <div>

                </div>

            <h5 style=" font-size: 9pt; padding-left: 22px;"><span>三级菜单</span></h5>

                <div>

                </div>

        </div>

    <h5 οnclick="Show(4)"><span>二级菜单</span></h5>

        <div id="div4" style="display: none;">

        </div>

<h4><span><a href="zyyw_ztjs.aspx?moduleid=937">一级菜单</a></span></h4>

    <h5 οnclick="Show(5)"><span>二级菜单</span></h5>

        <div id="div5" style="display: none;">

        </div>

    <h5 οnclick="Show(6)"><span>二级菜单</span></h5>

        <div id="div6" style="display: none;">

        </div>

    <h5 οnclick="Show(7)"><span>二级菜单</span></h5>

        <div id="div7" style="display: none;">

        </div>

Javascript方法a()调用位置:

<body οnlοad="a()">

                                                                                                                       --其中部分内容摘自互联网

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
常用的JS后台导航菜单,可刷新不变 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xml:lang="UTF-8" lang="UTF-8"> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <meta http-equiv="Content-Language" content="UTF-8" /> <title>后台管理</title> <style type="text/css"> body {margin:0;text-align:center;color:#000;font:normal 12px Arial,Verdana,Tahoma;text-align:center;background:#C8D0D5;line-height:150%;} a:link,a:visited {color:#385065;text-decoration:none} a:hover {text-decoration:underline} #menu {width:150px;margin:0px 15px;padding:0px;text-align:left;list-style:none} #menu .item {margin:5px 0px;padding:0px;list-style:none} a.title:link, a.title:visited, a.title:hover {display:block;background:url(http://www.tblog.com.cn/attachments/month_0609/m20069110491.gif) no-repeat;color:#385065;font-weight:bold;padding:2px 0 0 22px;width:128px;line-height:23px;cursor:pointer;text-decoration:none} #menu .item ul {border:1px solid #9FACB7;margin:0;width:118px;padding:3px 0px 3px 30px;background:#fff;list-style:none;display:none} #menu .item ul li {display:block;} </style> [removed] // --- 获取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(); } [removed] = cookieString; } //--- 获取cookie function getCookie(sName) { var aCookie = [removed].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 { 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] { 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"; } } } } [removed] </head> <body> <ul id="menu"> <li class="item"><a href="[removed]void(0)" class="title" name="1">基本信息</a> <ul id="opt_1" class="optiton"> <li><a href="#">企业简介</a></li> <li><a href="#">企业新闻</a></li> </ul> </li> <li class="item"><a href="[removed]void(0)" class="title" name="2">系统管理</a> <ul id="opt_2" class="optiton"> <li><a href="#">企业简介</a></li> <li><a href="#">企业新闻</a></li> </ul> </li> <li class="item"><a href="[removed]void(0)" class="title" name="3">系统管理</a> <ul id="opt_3" class="optiton"> <li><a href="#">会员管理</a></li> <li><a href="#">管理员设置</a></li> <li><a href="#">权限组设置</a></li> <li><a href="#">退出系统</a></li> </ul> </li> </ul> </body> </html> <a href="http://js.alixixi.com/">欢迎访问阿里西西网页特效代码站,js.alixixi.com</a>

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值