你这个效果的下拉菜单,不需要用 JavaScript,只用CSS就能实现。
加上一句
li:hover ul{ display:block}
就行了。
<!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/htmlcharset=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
}
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
}
li:hover ul{ display:block}
</style>
</head>
<body>
<div id="nav">
<ul>
<li><a href="#">首页</a></li>
<li><a href="#">课程大厅</a>
<ul>
<li><a href="#">JavaScript</a></li>
<li><a href="#">Html/CSS</a></li>
</ul>
</li>
<li><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>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"><html>
<head>
<meta http-equiv="Content-Type" content="text/htmlcharset=utf-8">
<title>无标题文档</title>
<style type="text/css">
*{ padding:0margin:0list-style:none}
.all{ width:330pxheight:30pxbackground:url(img/bg.jpg) no-repeatmargin:100px autoline-height:30pxtext-align:centerpadding-left:10pxmargin-bottom:0}
.all ul li{ width:100pxheight:30pxbackground:url(img/libg.jpg)float:leftmargin-right:10pxposition:relativecursor:pointer}
.all ul ul{ position:absoluteleft:0top:30pxdisplay:none}
</style>
</head>
<body>
<div class="all" id="list">
<ul>
<li>一级菜单
<ul>
<li>二级菜单</li>
<li>二级菜单</li>
<li>二级菜单</li>
</ul>
</li>
<li>一级菜单
<ul>
<li>二级菜单</li>
<li>二级菜单</li>
<li>二级菜单</li>
</ul>
</li>
<li>一级菜单
<ul>
<li>二级菜单</li>
<li>二级菜单</li>
<li>二级菜单</li>
</ul>
</li>
</ul>
</div>
</body>
</html>
<script>
// 获取对象 遍历对象操作 显示模块 隐藏模块
function List(id) { // 获取对象
this.id = document.getElementById(id) // 取 id 值
this.lis = this.id.children[0].children // 获取一级菜单所有的li
}
// init 初始化
List.prototype.init = function() { // 遍历所有的li 显示和隐藏
var that = this
for(var i=0i<this.lis.lengthi++)
{
this.lis[i].index = i
this.lis[i].onmouseover = function() {
that.show(this.children[0]) // 显示出来
}
this.lis[i].onmouseout = function() {
that.hide(this.children[0]) // 隐藏起来
}
}
}
// 显示模块
List.prototype.show = function(obj) {
obj.style.display = "block"
}
// 隐藏模块
List.prototype.hide = function(obj) {
obj.style.display = "none"
}
var list = new List("list") // 实例化了一个对象 叫 list
list.init()
</script>
<style>
.clearfix::after {
display: block
content: ""
clear: both
}
ul,
li {
list-style: none
}
.nav>li {
float: left
margin: 0 10px
}
.nav>li ul {
display: none
}
</style>
</head>
<body>
<ul class="nav clearfix">
<li>
<a href="">微博</a>
<ul>
<li>私信</li>
<li>评论</li>
<li>@我</li>
</ul>
</li>
<li>
<a href="">微博</a>
<ul>
<li>私信</li>
<li>评论</li>
<li>@我</li>
</ul>
</li>
<li>
<a href="">微博</a>
<ul>
<li>私信</li>
<li>评论</li>
<li>@我</li>
</ul>
</li>
<li>
<a href="">微博</a>
<ul>
<li>私信</li>
<li>评论</li>
<li>@我</li>
</ul>
</li>
</ul>
<script>
//案例分析:导航栏里面的li都要有鼠标经过效果 所以需要循环注册鼠标事件
//核心原理:当鼠标经过li里面的第二个孩子ul显示 当鼠标离开 ul隐藏
var nav = document.querySelector('.nav')
var lis = nav.children
for (var i = 0 i < lis.length i++) {
lis[i].onmouseover = function() {
this.children[1].style.display = 'block'
}
lis[i].onmouseout = function() {
this.children[1].style.display = 'none'
}
}
</script>