search

Home  >  Q&A  >  body text

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

布局看成上下两个盒子,鼠标移入上面的盒子,下面的盒子就显示。只有鼠标移出下面的盒子时,下面的盒子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 p {
            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 op = nav.getElementsByTagName("p")[0];
            var oUl = nav.getElementsByTagName("ul")[0];
            var arrLi = oUl.getElementsByTagName("li");
            var len = arrLi.length;

            navItem.onmouseover = function () {
                op.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";
                }
            }

            op.onmouseout = function () {
                op.style.display = "none";
            }
        }
    </script>
</head>
<body>
    <ul class="nav" id="nav">
        <li class="nav_item"><a href="#">卖家中心</a></li>
        <p>
            <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>
        </p>
    </ul>
</body>
</html>
PHP中文网PHP中文网2824 days ago392

reply all(2)I'll reply

  • 巴扎黑

    巴扎黑2017-04-10 17:09:47

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

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

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

     var nav = document.getElementById("nav");
                var navItem = nav.getElementsByTagName("li")[0];
                var op = nav.getElementsByTagName("p")[0];
                var oUl = nav.getElementsByTagName("ul")[0];
                var arrLi = oUl.getElementsByTagName("li");
                var len = arrLi.length;
    
                navItem.onmouseover = function () {
                    op.style.display = "block";
                }
                //add  顶部菜单移出 隐藏
                navItem.onmouseout = function () {
                    op.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状态下 继续显示
                op.onmouseover = function () {
                    op.style.display = "block";
                }
                op.onmouseout = function () {
                    op.style.display = "none";
                }

    reply
    0
  • 伊谢尔伦

    伊谢尔伦2017-04-10 17:09:47

    首先楼主下面这段可以用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 op = nav.getElementsByTagName("p")[0];
                var oUl = nav.getElementsByTagName("ul")[0];
                var arrLi = oUl.getElementsByTagName("li");
                var len = arrLi.length;
    
                navItem.onmouseover = function () {
                    op.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";
                    }
                }
    
                op.onmouseover = function () {
                    op.style.display = "block";
                }
    
                op.onmouseout = function () {
                    op.style.display = "none";
                }
            }
        </script>
        

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

    reply
    0
  • Cancelreply