添加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/