gpt4 book ai didi

jquery - 当我将 mousemove 事件绑定(bind)到父级时,如何阻止子级响应 mousemove 事件?

转载 作者:行者123 更新时间:2023-12-01 04:23:27 26 4
gpt4 key购买 nike

<div id="father" style="top:10px;left:10px;width:500px;height:500px">
<div id="child" style="position:relative;top:50px;left:50px;width:100px;height:100px">
</div>
</div>

$("#father").mousemove(function(){
alert("out");
})

如何只在父级上绑定(bind) mousemove 事件,而不继承子级?当我在父图层中移动鼠标时,可以调用该函数,但在子图层中则不能调用该函数。 PS:我不需要子层上的 mouseout 事件,因为可能有许多相邻的子层。子层之间的交叉事件不需要触发该函数。

最佳答案

你的javascript有mouseenter,但是你的标题和描述正在谈论mousemove..所以我假设你的意思是mousemove。处理此问题的一种方法是仅当父级具有特定类时才将函数绑定(bind)到 mousemouse 事件。与“active”类似,当鼠标移到子级上时,从父级中删除事件类。

<强> Here is a fiddle to test

$('#child').mouseenter(function() {
$(this).parent().removeClass('active')
}).mouseleave(function() {
$(this).parent().addClass('active')
});
$('body').delegate("#father.active", "mousemove", function() {
console.log("moving");
})

关于jquery - 当我将 mousemove 事件绑定(bind)到父级时,如何阻止子级响应 mousemove 事件?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8513887/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com