javascript - 为什么子元素悬停时会触发 mouseout?

标签 javascript

令人惊奇的是当鼠标光标移到子节点上时会触发 mouseout 事件。为什么光标没有离开父节点会出现这种情况呢?请考虑这一点:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" 
    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="content-type" content="text/html; charset=utf-8" />
<title></title>
<script type='text/javascript'>

    function someFunc(obj) {
        document.getElementById('info').innerHTML = 'over';
        document.getElementById('info3').innerHTML = 'over';

        obj.onmouseout = function() { 
            document.getElementById('info2').innerHTML = 'out';
            document.getElementById('info3').innerHTML = 'out';
        }

        if (!obj.theChild) {
            var theChild = document.createElement('div');
            theChild.style.position = 'relative';
            theChild.style.left = '20px';
            theChild.style.top = '20px';
            theChild.style.width = '40px';
            theChild.style.height = '40px';
            theChild.style.background = '#eeee00';
            obj.theChild = theChild;
            obj.appendChild(theChild);
        }
    }

</script> 
<head>
<body>

    <span id="info"></span> <span id="info2"></span> <span id="info3"></span>

    <div style="position:absolute;top:100px;left:100px;width:100px;height:100px;background:#000000;" onmouseover="someFunc(this);"></div>

</body>
</html>

最佳答案

关于javascript - 为什么子元素悬停时会触发 mouseout?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4093416/

相关文章:

javascript - 如何为 table.rows.length 函数排除 html 表中的标题行?

javascript - 日期时间从 UTC 转换为其他时区

javascript - 如何获取整个 html 内容并存储在变量中以便在我的站点中重用

javascript - Angular JS 在加载 href 资源之前触发 ng-click

javascript - 如何根据字段从 javascript 对象中删除一行并重新对齐对象

javascript - 编辑分析跟踪器配置

javascript - 在这种情况下 'pass result to parameter' 意味着什么?

javascript - 在客户端 javascript 文件中使用 api key 有多安全?可以轻松地使用此 api key 在我的应用程序上创建对象吗?

javascript - 我如何告诉 Grunt 不要在构建任务中缩小或连接 js 文件?

javascript - 组对象数组下划线js