码迷,mamicode.com
首页 > Web开发 > 详细

竖向折叠二级导航JS代码(可防刷新ul/li结构)

时间:2014-10-09 14:23:23      阅读:223      评论:0      收藏:0      [点我收藏+]

标签:style   http   color   io   os   ar   java   for   sp   


<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
      <style type="text/css">
        *{margin:0;padding:0;border:0;}
        body
        {
         font-family: arial, 宋体, serif;
         font-size:12px;
        }
        #nav {
         width:180px;
         line-height: 24px;
         list-style-type: none;
         text-align:left;
            /*定义整个ul菜单的行高和背景色*/
        }
        /*==================一级目录===================*/
        #nav a {
         width: 160px;
         display: block;
         padding-left:20px;       
        }
        #nav li {
         background:#CCC; /*一级目录的背景色*/
         border-bottom:#FFF 1px solid; /*下面的一条白边*/
         float:left;        
        }
        #nav li a:hover{
         background:#CC0000; /*一级目录onMouseOver显示的背景色*/
        }
        #nav a:link  {
         color:#666; text-decoration:none;
        }
        #nav a:visited  {
         color:#666;text-decoration:none;
        }
        #nav a:hover  {
         color:#FFF;text-decoration:none;font-weight:bold;
        }
        /*==================二级目录===================*/
        #nav li ul {
         list-style:none;
         text-align:left;
        }
        #nav li ul li{
         background: #EBEBEB; /*二级目录的背景色*/
        }
        #nav li ul a{
            padding-left:20px;
            width:160px;
         /* padding-left二级目录中文字向右移动,但Width必须重新设置=(总宽度-padding-left)*/
        }
        /*下面是二级目录的链接样式*/
        #nav li ul a:link  {
         color:#666; text-decoration:none;
        }
        #nav li ul a:visited  {
         color:#666;text-decoration:none;
        }
        #nav li ul a:hover {
         color:#F3F3F3;
         text-decoration:none;
         font-weight:normal;
         background:#CC0000;
         /* 二级onmouseover的字体颜色、背景色*/
        }
        /*==============================*/
        #nav li:hover ul {
         left: auto;
        }
        #nav li.sfhover ul {
         left: auto;
        }
        #content {
         clear: left;
        }
        #nav ul.collapsed {
         display: none;
        }
        #PARENT{
         width:300px;
         padding-left:20px;
        }
</style>

<script type="text/javascript">
    var LastLeftID = "";
    function menuFix() {
        var obj = document.getElementById("nav").getElementsByTagName("li");

        for (var i = 0; i < obj.length; i++) {
            obj[i].onmouseover = function () {
                this.className += (this.className.length > 0 ? " " : "") + "sfhover";
            }
            obj[i].onMouseDown = function () {
                this.className += (this.className.length > 0 ? " " : "") + "sfhover";
            }
            obj[i].onMouseUp = function () {
                this.className += (this.className.length > 0 ? " " : "") + "sfhover";
            }
            obj[i].onmouseout = function () {
                this.className = this.className.replace(new RegExp("( ?|^)sfhover\\b"), "");
            }
        }
    }
    function DoMenu(emid) {
        var obj = document.getElementById(emid);
        obj.className = (obj.className.toLowerCase() == "expanded" ? "collapsed" : "expanded");
        if ((LastLeftID != "") && (emid != LastLeftID)) //关闭上一个Menu
        {
            document.getElementById(LastLeftID).className = "collapsed";
        }
        LastLeftID = emid;
    }
    function GetMenuID() {
        var MenuID = "";
        var _paramStr = new String(window.location.href);
        var _sharpPos = _paramStr.indexOf("#");

        if (_sharpPos >= 0 && _sharpPos < _paramStr.length - 1) {
            _paramStr = _paramStr.substring(_sharpPos + 1, _paramStr.length);
        }
        else {
            _paramStr = "";
        }

        if (_paramStr.length > 0) {
            var _paramArr = _paramStr.split("&");
            if (_paramArr.length > 0) {
                var _paramKeyVal = _paramArr[0].split("=");
                if (_paramKeyVal.length > 0) {
                    MenuID = _paramKeyVal[1];
                }
            }
            /*
            if (_paramArr.length>0)
            {
            var _arr = new Array(_paramArr.length);
            }
 
            //取所有#后面的,菜单只需用到Menu
            //for (var i = 0; i < _paramArr.length; i++)
            {
            var _paramKeyVal = _paramArr[i].split(‘=‘);
   
            if (_paramKeyVal.length>0)
            {
            _arr[_paramKeyVal[0]] = _paramKeyVal[1];
            }  
            }
            */
        }

        if (MenuID != "") {
            DoMenu(MenuID)
        }
    }
    GetMenuID(); //*这两个function的顺序要注意一下,不然在Firefox里GetMenuID()不起效果
    menuFix();
 </script>
</head>
<body>

<div id="PARENT">
<ul id="nav">
<li><a href="#Menu=ChildMenu1"  onmouseover="DoMenu(‘ChildMenu1‘)" onclick="DoMenu(‘ChildMenu1‘)">懒人建站</a>
 <ul id="ChildMenu1" class="collapsed">
 <li><a href="#">懒人建站</a></li>
 <li><a href="#">js代码</a></li>
 <li><a href="#">懒人建站</a></li>
 <li><a href="#">js代码</a></li>
 <li><a href="#">懒人建站</a></li>
 <li><a href="#">懒人建站</a></li>
 </ul>
</li>
<li><a href="#Menu=ChildMenu2" onclick="DoMenu(‘ChildMenu2‘)">懒人建站</a>
 <ul id="ChildMenu2" class="collapsed">
 <li><a href="#">懒人建站</a></li>
 <li><a href="#">懒人建站</a></li>
 <li><a href="#">懒人建站</a></li>
 <li><a href="#">懒人建站</a></li>
 <li><a href="#">懒人建站</a></li>
 <li><a href="#">懒人建站</a></li>
 </ul>
</li>
<li><a href="#Menu=ChildMenu3" onclick="DoMenu(‘ChildMenu3‘)">懒人建站</a>
 <ul id="ChildMenu3" class="collapsed">
 <li><a href="#">懒人建站</a></li>
 <li><a href="#">懒人建站</a></li>
 <li><a href="#">懒人建站</a></li>
 <li><a href="#">懒人建站</a></li>
 </ul>
</li>
<li><a href="#Menu=ChildMenu4" onclick="DoMenu(‘ChildMenu4‘)">懒人建站</a>
 <ul id="ChildMenu4" class="collapsed">
 <li><a href="#">懒人建站</a></li>
 <li><a href="#">懒人建站</a></li>
 <li><a href="#">懒人建站</a></li>
 <li><a href="#">懒人建站</a></li>
 <li><a href="#">懒人建站</a></li>
 </ul>
</li>
</ul>
</div>
</body>
</html>

 

转载原文:http://www.51xuediannao.com/js/nav/254.html

 

竖向折叠二级导航JS代码(可防刷新ul/li结构)

标签:style   http   color   io   os   ar   java   for   sp   

原文地址:http://www.cnblogs.com/lizihong/p/4012802.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!