简体   繁体   English

停止在子节点上触发事件

[英]Stop events triggering on the child nodes

I have written a program for a drop down menu, I want the code to go from display block to none on a mouse out but the mouse out event is triggered even on its child nodes I want to prevent that from happening what are the possible solutions ? 我已经为下拉菜单编写了一个程序,我希望代码在mouse out从显示block变为none ,但是即使在其子节点上也会触发mouse out事件我希望防止它发生什么是可能的解决方案?


CSS CSS

.dropdown{
        list-style: none;
        display: none;
        position: absolute; 
        background-color: skyblue;
    }

HTML HTML

<button>Home</button>
    <div class="dropdown">
        <ul class="">
            <li><a href="">Home</a></li>
            <li><a href="">Contacts</a></li>
            <li><a href="">Other</a></li>
        </ul>
    </div>

    <script type="text/javascript">

        var button = document.getElementsByTagName('button')[0];
        var dropdown = document.getElementsByClassName('dropdown')[0];
        var ddChildNodes = document.querySelector('li');

        button.addEventListener('mouseover',function(){
            dropdown.style.display='block';
        },false);

        dropdown.addEventListener('mouseout',
            function(){
                    dropdown.style.display='none';
            },false);
    </script>

With most modern browsers (and even ancient IE), you can use mouseenter and mouseleave instead of mouseover and mouseout . 对于大多数现代浏览器 (甚至是古老的IE),您可以使用mouseentermouseleave而不是mouseovermouseout mouseenter and mouseleave don't bubble. mouseentermouseleave不起泡。

Support: 支持:

  • IE 5.5+ IE 5.5+
  • Firefox 10+ Firefox 10+
  • Chrome 30+ Chrome 30+

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM