下拉菜单——在父节点中设置时,onmouseout 在子节点上被调用

发布于 2024-12-26 23:24:52 字数 1543 浏览 5 评论 0原文

尝试模仿以下链接中的浏览类别https://dev.twitter.com/讨论

onmouseover - 容器扩展以适应自身内的新项目 - 但是,在容器内移动鼠标(扩展容器)将导致 onmouseout 被调用- 即使鼠标位于容器本身内 - 愚蠢的错误或没有努力找出我可能出错的位置和方式

代码 -

<html>
    <style type="text/css">
    #container{
    overflow: hidden;
    height: auto;
    width: 350px;
    background-color: rgba(0,0,0,0.65);
    }
    .contents{
    height: 30px;
    width: 350px;
    background-color: yellow;
    float: left;
    }
    </style>
    <script type="text/javascript" >
    var foo = new Array("bar","santa","claus")
    function fire(){
    var contents = document.getElementById("contents")
    if(contents.hasChildNodes())
    return
    for(i = 0 ; i < foo.length ; i++){
        var tem=document.createElement("div");
        tem.setAttribute("id",'cont'+i);
        tem.setAttribute("class","contents");
        tem.appendChild(document.createTextNode(foo[i]))
        contents.appendChild(tem)
    }
    }
    function unfire(evt){

    if ((evt.target || evt.srcElement).id != "container") 
    return;

    var contents = document.getElementById("contents");
    while(contents.hasChildNodes())
    contents.removeChild(contents.firstChild)
    }
    </script>

    <div id="container" onmouseover="fire(event)" onmouseout="unfire(event)">
        Move your mouse here
        <div id="contents" ></div>
    </div>
    </html>

Trying to mimic the browse catagories in the following link https://dev.twitter.com/discussions

onmouseover -- the container expands to fit the new items within itself -- but,moving the mouse within the container(expanded conainer) will result in the onmouseout getting invoked -- even when the mouse is within the container itself -- silly mistake or not trying hard to find out where and how i might be going wrong

Code --

<html>
    <style type="text/css">
    #container{
    overflow: hidden;
    height: auto;
    width: 350px;
    background-color: rgba(0,0,0,0.65);
    }
    .contents{
    height: 30px;
    width: 350px;
    background-color: yellow;
    float: left;
    }
    </style>
    <script type="text/javascript" >
    var foo = new Array("bar","santa","claus")
    function fire(){
    var contents = document.getElementById("contents")
    if(contents.hasChildNodes())
    return
    for(i = 0 ; i < foo.length ; i++){
        var tem=document.createElement("div");
        tem.setAttribute("id",'cont'+i);
        tem.setAttribute("class","contents");
        tem.appendChild(document.createTextNode(foo[i]))
        contents.appendChild(tem)
    }
    }
    function unfire(evt){

    if ((evt.target || evt.srcElement).id != "container") 
    return;

    var contents = document.getElementById("contents");
    while(contents.hasChildNodes())
    contents.removeChild(contents.firstChild)
    }
    </script>

    <div id="container" onmouseover="fire(event)" onmouseout="unfire(event)">
        Move your mouse here
        <div id="contents" ></div>
    </div>
    </html>

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

一杆小烟枪 2025-01-02 23:24:53

抱歉,我原来的答案完全错误,我不确定我在想什么。当然,当鼠标移动到子级时,mouseout 会在父级上触发。在这种情况下,您需要检查 event 对象的 latedTargettoElement 属性,并检查该元素是否是容器。

您可以在 Internet Explorer 中使用 contains() 检查祖先,在其他浏览器中使用 compareDocumentPosition()。例如,将 onmouseout="unfire(event)" 更改为 onmouseout="unfire.call(this, event)" 并将以下代码添加到 unfire功能:

var to = evt.relatedTarget || evt.toElement;

if((this.contains && this.contains(to)) || this.compareDocumentPosition(to) & 16)
    return;

Sorry, I got my original answer completely wrong, I'm not sure what I was thinking. Of course, mouseout fires on a parent when the mouse moves to a child. In this case, you need to check the relatedTarget or toElement properties of the event object and check to see if that element is a descendant of the container.

You can check ancestry using contains() in Internet Explorer and compareDocumentPosition() in other browsers. For example, change onmouseout="unfire(event)" to onmouseout="unfire.call(this, event)" and add the following code to the unfire function:

var to = evt.relatedTarget || evt.toElement;

if((this.contains && this.contains(to)) || this.compareDocumentPosition(to) & 16)
    return;
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文