手记

实现单击一级菜单显示或隐藏二级菜单

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
<style>

ul1{
        width: 200px;
        height: auto;
        border: 1px solid lightsalmon;
    }
    ul >li{
        list-style: none;
        position: relative;
    }
    ul>li>ul{
        margin: 0px auto;
    }
    ul >li>a {
        text-decoration: none;
    }
    .menu{
        display: none;
    }
</style>
<script>
    window.onload= function () {
      var AllMenuLi=document.getElementsByClassName('first');
       var i=0;
        for(i=0;i<AllMenuLi.length;i++)
        {
            AllMenuLi[i].onclick= function () {
              //  alert("sss");
               var UlMenuLi=this.getElementsByTagName("ul")[0];
                if(UlMenuLi.className=='menu')
                {
                 UlMenuLi.className='';
                }
                else {
                    UlMenuLi.className='menu';
                }
            };
        }
    };
</script>

</head>
<body>
<ul id="ul1">
<li><a href="#">about us</a></li>
<li><a href="#" >learn more</a></li>
<li class="first"><a href="#">show information</a>
<ul class="menu">
<li><a href="#">asdksdk</a> </li>
<li><a href="#">asdksdk</a> </li>
<li><a href="#">asdksdk</a> </li>
</ul>
</li>
<li><a href="#">dddkssss</a> </li>
<li class="first"><a href="#">dkkkkk</a>
<ul class="menu">
<li><a href="#">aaaaasdk</a> </li>
<li><a href="#">aaavvvv</a> </li>
<li><a href="#">ccccccc</a> </li>
</ul>
</li>
</ul>
</body>
</html>

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