手记

javascript 下拉菜单的不同写法

<!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=utf-8" />
<title>无标题文档</title>
<style type="text/css">
*{ margin:0px; padding:0px;}
body{ font-family:Verdana, Geneva, sans-serif; font-size:14px;}
.nav{ width:600px; height:40px; background-color:#eee; margin:0 auto; margin-top:150px;}
ul{ list-style:none;}
ul li{ float:left; line-height:40px; text-align:center; width:100px;}
a{ text-decoration:none; color:#000; display:block;}
a:hover{ color:#F00; background-color:#666;}
ul li ul li{ float:none;background-color:#eee; margin:2px 0px;}
ul li ul{display:none;}
ul li ul a:hover{background-color:#c00;color:#fff;}

menu02{background-color:#222;}
menu02 a{font-family:"Microsoft YaHei",sans-serif; color:#fff;}
menu02 a:hover{background-color:#c00;color:#FFF;font-weight:blod;}
menu02 li ul li{background-color:#222;}

</style>
<script type="text/javascript">
/*
function ShowSub(li) {
var subMenu = li.getElementsByTagName("ul")[0];
subMenu.style.display = "block";
}

    function HideSub(li) {
        var subMenu = li.getElementsByTagName("ul")[0];
        subMenu.style.display = "none";
    }

*/

    window.onload=function(){
        var sub = getClass("subnav");
        //alert(sub[0].innerHTML);
        for(var i=0,len=sub.length; i<len; i++){
          addEvent(sub[i],"mouseover",function(){
            var ul = this.getElementsByTagName("ul")[0];
            ul.style.display="block";
          });
          addEvent(sub[i],"mouseout",function(){
            var ul = this.getElementsByTagName("ul")[0];
            ul.style.display="none";
          });
        }
    }
    //获取class集
    function getClass(cls,obj) {
        var obj = obj?obj:document;
        if(document.getElementsByClassName){
            return obj.getElementsByClassName(cls);
        }else{
            var all = obj.getElementsByTagName("*"),
                result = [];
            for(var i=0,len=all.length; i<len; i++){
                if(all[i].className === cls){
                    result.push(all[i]);
                }
            }
            return result;
        }
    }
    //添加事件
    function addEvent(o,type,fn) {
      if(o.addEventLister){
        o.addEventLister(type,fn,false);//false不冒泡
      }else if(o.attachEvent){
        o.attachEvent("on"+type,fn);
      }else{
        o["on"+type]=fn;
      }
    }

</script>

</head>

<body>
<!--
<div class="nav">
<ul>
<li><a href="#">首页</a></li>
<li onmouseover="ShowSub(this)" onmouseout="HideSub(this)" ><a href="#">课程大厅</a>
<ul>
<li><a href="#">JavaScript</a></li>
<li><a href="#">Html/CSS</a></li>
</ul>
</li>
<li onmouseover="ShowSub(this)" onmouseout="HideSub(this)" ><a href="#">学习中心</a>
<ul>
<li><a href="#">视频学习</a></li>
<li><a href="#">实例练习</a></li>
<li><a href="#">问与答</a></li>
</ul>

</li>
<li><a href="#">经典案例</a></li>
<li><a href="#">关于我们</a></li>
</ul>
</div>
-->
<div class="nav" id="menu02">
<ul>
<li><a href="#">首页</a></li>
<li class="subnav"><a href="#">课程大厅</a>
<ul>
<li><a href="#">JavaScript</a></li>
<li><a href="#">Html/CSS</a></li>
</ul>
</li>
<li class="subnav"><a href="#">学习中心</a>
<ul>
<li><a href="#">视频学习</a></li>
<li><a href="#">实例练习</a></li>
<li><a href="#">问与答</a></li>
</ul>

</li>
<li><a href="#">经典案例</a></li>
<li><a href="#">关于我们</a></li>
</ul>

</div>
</body>
</html>

0人推荐
随时随地看视频
慕课网APP