鼠标进入和离开事件

   <style>
        li {
            list-style: none;
            cursor: pointer;
        }
    </style>
</head>
<body>
<ul id="uu">
    <li>1</li>
    <li>2</li>
    <li>3</li>
</ul>

<script src="learn2.js"></script>
<script>
    var liObjs = document.getElementsByTagName("li");
    for (var i = 0; i < liObjs.length; i++) {
        //为li注册鼠标进入事件
        liObjs[i].onmousemove = function () {
            //将颜色设置为黄色
            this.style.backgroundColor = "yellow";
        };
        //为li注册鼠标离开事件
        liObjs[i].onmouseout = function () {
            //恢复为默认的颜色
            this.style.backgroundColor = "";
        };
    }

</script>
</body>
©️2020 CSDN 皮肤主题: 大白 设计师:CSDN官方博客 返回首页