js 简单下拉菜单

<style>
        * {
            margin: 0;
            padding: 0;
        }
        
        .main {
            margin-left: 20px;
            margin-top: 20px;
        }
        
        ul {
            list-style: none;
            display: none;
        }
        
        h4 {
            position: relative;
            display: block;
            width: 76px;
            height: 40px;
            font-size: 15px;
            text-align: center;
            line-height: 40px;
        }
        
        span {
            display: block;
            position: absolute;
            top: 15px;
            right: 5px;
            background: url(images/icon.png) no-repeat 0 -976px;
            width: 10px;
            height: 10px;
        }
        
        ul li {
            width: 76px;
            height: 40px;
            text-align: center;
            line-height: 40px;
            border: 1px solid orange;
            font-size: 15px;
            background-color: #fff;
            color: #333;
        }
    </style>
<div class="main">
        <h4>微博<span></span></h4>
        <ul>
            <li>私信</li>
            <li>评论</li>
            <li>@我</li>
        </ul>
</div>

    <script>
        var getul = document.querySelector('ul');     //获取ul对象
        var lis = document.querySelectorAll('li');	  //获取li对象
        var geth = document.querySelector('h4');      //获取h4对象
        var gets = document.querySelector('span');    //获取span对象
        geth.onclick = function() {                   //点击h4对象
            getul.style.display = 'block';            //显示ul
            this.style.backgroundColor = 'rgba(204,204,204)';  //更换h4背景颜色
        }
        for (var i = 0; i < lis.length; i++) {        //遍历li
            lis[i].onmouseover = function() {         //鼠标放在li上面
                this.style.color = 'red';
                this.style.backgroundColor = '#fff5da'
            }
            lis[i].onmouseout = function() {           //鼠标离开li
                this.style.color = '#4c4c4c';
                this.style.backgroundColor = '';
            }
        }
   </script>

在这里插入图片描述
在这里插入图片描述在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值