<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8"/>
<meta name="viewport" content="width=device-width,initial-scale=1.0">
<title>导航下拉菜单</title>
<style>
*{
margin:0px;
padding:0px;
box-sizing:border-box;
}
a{
color:red;
text-decoration:none;
}
#nav{
background:black;
height:50px;
line-height:50px;
}
li{
list-style:none;
margin:0px 10px;
float:left;
}
#nav>li>a:hover{
color:white;
}
#nav>li{
position:relative;
}
#nav>li>ul{
position:absolute;
top:50px;
width:180px;
border:1px solid #aaa;
border-top:none;
}
#nav>li>url>li a{
display:line-block;
height:50px;
color:#444;
}
ul.sub li:hover{
background:#eee;
}
#nav>li>ul{
display:none;
}
</style>
</head>
<body>
<ul id="nav">
<li><a href="">首页</a></li>
<li><a href="">视频教程</a></li>
<li>
<a href="">资源下载</a>
<ul>
<li><a href="">php手册</a></li>
<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>
<ul>
<li><a href="">php手册</a></li>
<li><a href="">在线课程</a></li>
<li><a href="">学习工具</a></li>
<li><a href="">网站源码</a></li>
</ul>
</li>
</ul>
</body>
<script>
const navs=document.querySelectorAll(“#nav>li”);
navs.forEach(function(nav){
nav.addEventListener(“mouseover”,showSubMenu);
nav.addEventListener(“mouseout”,closeSubMenu);
});
function showSubMenu(ev){
if(ev.target.nextElementSibling !== null){
ev.target.nextElementSibling.style.display=”block”;
}}
function closeSubMenu(ev){
if(ev.target.nodeName ==”A” && ev.target.nextElementSibling !== null){
ev.target.nextElementSibling.style.display=”none”;
}}
</script>
</html>