原生js实现下拉菜单
Posted 轻笑苍穹
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了原生js实现下拉菜单相关的知识,希望对你有一定的参考价值。
简单给两段代码:
html和css部分:
<style>
.one li{ width:100px; height:30px; line-height:30px; background:url(nav/libg.jpg) no-repeat; float:left; text-align:center; margin-left:10px; cursor:pointer; _display:inline; position:relative;}
li ul{ position:absolute; left:0; top:30px; display:none;}
</style>
<ul class="one"> <li id="li01"> <span>一级菜单</span> <ul id="ul01"> <li>二级菜单</li> <li>二级菜单</li> <li>二级菜单</li> </ul> </li> <li id="li02"> <span>一级菜单</span> <ul id="ul02"> <li>二级菜单</li> <li>二级菜单</li> <li>二级菜单</li> </ul> </li> <li id="li03"> <span>一级菜单</span> <ul id="ul03"> <li>二级菜单</li> <li>二级菜单</li> <li>二级菜单</li> </ul> </li> </ul>
javascript部分:
function fn(canshu1,canshu2){ var li01 = document.getElementById(canshu1); var ul01 = document.getElementById(canshu2); li01.onmouseover = function(){ ul01.style.display = "block" } li01.onmouseout = function(){ ul01.style.display = "none" } }
fn("li01","ul01")
fn("li02","ul02")
fn("li03","ul03")
亮个效果图:
以上是关于原生js实现下拉菜单的主要内容,如果未能解决你的问题,请参考以下文章