效果:
代码:
<!DOCTYPE html>
<html>
<head>
<title>js实现下拉列表</title>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<style type="text/css">
*{margin: 0px;padding: 0px}
.nav{background-color: #eee;width: 600px;height: 40px;margin: 0 auto;}
.nav-ul{list-style: none;}
.nav-ul li{float: left;line-height: 40px;text-align: center;position: relative;}
.nav-ul li a {text-decoration: none;color: #000;display: block;padding:0 10px;}
.nav-ul li a:hover{color: #FFF;background-color: #666}
.nav-ul li .nav-ul-li-ul{position: absolute;left: 0px; top:40px;display: none;}
.nav-ul li .nav-ul-li-ul li {list-style: none; float: none;background-color: #eee;margin-top: 2px;}
.nav-ul li .nav-ul-li-ul li a:hover{color: #FFF;background-color: #36e}
</style>
<script type="text/javascript">
/*
鼠标离开事件
*/
function hidesubment (li) {
//获取子菜单对象
var submenuobj = li.getElementsByClassName("nav-ul-li-ul")[0];
//设置显示的属性
submenuobj.style.display = "none";
}
/*
鼠标经过事件
*/
function showsubmenu (li) {
//获取对象
var submenuobj = li.getElementsByClassName("nav-ul-li-ul")[0];
//设置显示的属性
submenuobj.style.display = "block";
}
</script>
</head>
<body>
<div class="nav">
<ul class="nav-ul">
<li><a href="">首页</a></li>
<li onmouseout="hidesubment(this)">
<a href="">我的课程</a>
<ul class="nav-ul-li-ul">
<li><a href="">java</a></li>
<li><a href="">php</a></li>
</ul>
</li>
<li><a href="">关于我们</a></li>
</ul>
</div>
</body>
</html>