首页 > 还是有关鼠标移入移出的问题,欢迎热心的朋友帮我看看哪出问题了

还是有关鼠标移入移出的问题,欢迎热心的朋友帮我看看哪出问题了

布局看成上下两个盒子,鼠标移入上面的盒子,下面的盒子就显示。只有鼠标移出下面的盒子时,下面的盒子display才为none,鼠标移入移出不是针对同一个事物 ,但我鼠标移出上面的盒子,下面的盒子就消失了,都还来不及在下面的盒子选选项。请问,我是思路不对还是语法错了?

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>卖家中心</title>
    <link rel="shortcut icon" href="favicon.ico">
    <style>
        * {
            margin: 0;
            padding: 0;
        }
        li {
            list-style: none;
        }
        a {
            text-decoration: none;
            color: #000;
        }
        .nav {
            width: 110px;
            height: 30px;
            margin: 50px auto;
            background-color: #fffc9e;
        }
        .nav .nav_item {
            line-height: 30px;
        }
        .nav div {
            display: none;
        }
        .nav ul li {
            line-height: 28px;
        }
    </style>
    <script>
        window.onload = function () {
            var nav = document.getElementById("nav");
            var navItem = nav.getElementsByTagName("li")[0];
            var oDiv = nav.getElementsByTagName("div")[0];
            var oUl = nav.getElementsByTagName("ul")[0];
            var arrLi = oUl.getElementsByTagName("li");
            var len = arrLi.length;

            navItem.onmouseover = function () {
                oDiv.style.display = "block";
            }


            for(var i=0;i<len;i++){
                arrLi[i].onmouseover = function () {
                    this.style.backgroundColor = "#ccc";
                }
                arrLi[i].onmouseout = function () {
                    this.style.backgroundColor = "#fff";
                }
            }

            oDiv.onmouseout = function () {
                oDiv.style.display = "none";
            }
        }
    </script>
</head>
<body>
    <ul class="nav" id="nav">
        <li class="nav_item"><a href="#">卖家中心</a></li>
        <div>
            <ul>
                <li><a href="#">免费开店</a></li>
                <li><a href="#">已卖出的宝贝</a></li>
                <li><a href="#">出售中的宝贝</a></li>
                <li><a href="#">卖家服务市场</a></li>
                <li><a href="#">卖家培训中心</a></li>
            </ul>
        </div>
    </ul>
</body>
</html>

首先楼主下面这段可以用css的伪元素:hover代替

for(var i=0;i<len;i++){
                arrLi[i].onmouseover = function () {
                    this.style.backgroundColor = "#ccc";
                }
                arrLi[i].onmouseout = function () {
                    this.style.backgroundColor = "#fff";
                }
            }
            

其次你的问题这样就达到你的目的

<script>
        window.onload = function () {
            var nav = document.getElementById("nav");
            var navItem = nav.getElementsByClassName("nav_item")[0];
            var oDiv = nav.getElementsByTagName("div")[0];
            var oUl = nav.getElementsByTagName("ul")[0];
            var arrLi = oUl.getElementsByTagName("li");
            var len = arrLi.length;

            navItem.onmouseover = function () {
                oDiv.style.display = "block";
            }


            for(var i=0;i<len;i++){
                arrLi[i].onmouseover = function () {
                    this.style.backgroundColor = "#ccc";
                }
                arrLi[i].onmouseout = function () {
                    this.style.backgroundColor = "#fff";
                }
            }

            oDiv.onmouseover = function () {
                oDiv.style.display = "block";
            }

            oDiv.onmouseout = function () {
                oDiv.style.display = "none";
            }
        }
    </script>
    

你还可以参考我以前的实现


我还 手撸的 。。 原生选择器毕竟太难受,不爱用~~~

// 顶部菜单 mouseover mouseout 
document.querySelector('.nav_item').addEventListener(
    'mouseover', function(){
      document.querySelector('#nav div').style.display = 'block';
    }
, false)

document.querySelector('.nav_item').addEventListener(
    'mouseout', function(){
      document.querySelector('#nav div').style.display = 'none';
    }
, false)

//子菜单 显示/隐藏
document.querySelector('#nav div').addEventListener(
    'mouseover', function(){
      document.querySelector('#nav div').style.display = 'block';
    }
, false)

document.querySelector('#nav div').addEventListener(
    'mouseout', function(){
      document.querySelector('#nav div').style.display = 'none';
    }
, false)

添了两段, 和lz的意思一样, lz的代码问题 是 少了 两段~~

 var nav = document.getElementById("nav");
            var navItem = nav.getElementsByTagName("li")[0];
            var oDiv = nav.getElementsByTagName("div")[0];
            var oUl = nav.getElementsByTagName("ul")[0];
            var arrLi = oUl.getElementsByTagName("li");
            var len = arrLi.length;

            navItem.onmouseover = function () {
                oDiv.style.display = "block";
            }
            //add  顶部菜单移出 隐藏
            navItem.onmouseout = function () {
                oDiv.style.display = "none";
            }

            for(var i=0;i<len;i++){
                arrLi[i].onmouseover = function () {
                    this.style.backgroundColor = "#ccc";
                }
                arrLi[i].onmouseout = function () {
                    this.style.backgroundColor = "#fff";
                }
            }
            
            //子菜单 hover状态下 继续显示
            oDiv.onmouseover = function () {
                oDiv.style.display = "block";
            }
            oDiv.onmouseout = function () {
                oDiv.style.display = "none";
            }
【热门文章】
【热门文章】