google-chrome - 单击事件会影响 chrome 上的 mouseenter 和 mouseleave,这是一个错误吗?

标签 google-chrome click mouseenter mouseleave

添加mouseenter和mouseleave事件后,点击元素(点击次数不定),mouseenter和mouseleave会被触发。

我试了chrome 62.0.3202.94(32),firework,IE,只出现在chrome上。是chrome的bug,还是我的代码有问题?

在线示例:http://runjs.cn/code/cbb0aw1a

代码如下:

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>
<h4>click on the parent part,then see the console </h4>
<div class="parent" style="width: 100%;height: 100px;background-color: #ddd">
    <div class="children" style="width: 50px;height: 50px;background-color: #d9534f;cursor: pointer">
        test
    </div>
</div>
</body>
<script src="https://cdn.bootcss.com/jquery/3.2.1/jquery.js"></script>
<script>
    $(".parent").mouseenter(function () {
        console.log("enter")
    }).mouseleave(function () {
        console.log("leave")
    });

</script>
</html>

最佳答案

我可以确认 chrome 上的行为(版本 62.0.3202.94)。

多次单击一个元素时,有时会使用 MouseEvent 上的 relatedTarget/toElement 属性“null”触发 mouseleave(screenX、screenY 属性均为 0)。

行为是随机的,有时需要 2 次后续点击,有时超过 20 次。点击速度/速率似乎也无关紧要。

现在,我通过检查事件的 relatedTarget 属性(如果“null”什么都不做)来避免在我的 onMouseLeave(event) 方法中执行不需要的代码。不知道是否还有其他有用的情况,relatedTarget 可能为空...

关于google-chrome - 单击事件会影响 chrome 上的 mouseenter 和 mouseleave,这是一个错误吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47649442/

相关文章:

selenium - ERROR :ssl_client_socket_openssl. cc(1158)] ChromeDriver Chrome 浏览器和 Selenium 握手失败

java - 无法使用 Selenium 中的用户配置文件启动 Chrome 浏览器

javascript - 如何只删除最后一个字符而不是整个值?

javascript - jquery点击功能聚焦于textarea?

javascript - 使具有用户交互的 jQuery .mouseenter()/.mouseleave() 脚本工作时出现问题

jquery - Hover/mouseenter 事件重复触发 jQuery UI 的 'animate'

javascript - 奇怪的 Javascript 添加行为

jquery 单击时反转类属性

click - flot plotpan vs plotclick : how to ignore click event when panning is on?

javascript - 通过调用函数来清理 mouseenter 吗?