当我将 mousemove 事件绑定到父级时,如何阻止子级响应 mousemove 事件?
<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");
})
如何只在父级上绑定 mousemove 事件,而不继承子级? 当我在父图层中移动鼠标时,可以调用该函数,但在子图层中则不能调用该函数。 PS:我不需要子层上的 mouseout 事件,因为可能有许多相邻的子层。子层之间的交叉事件不需要触发该函数。
<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");
})
How do I only bind the mousemove event on the parent, not inherit the child?
When I move the mouse in the parent layer, I can call the function but not in the child layer. PS:I don't need the mouseout event on the child layer as could have many neighboring child layers. The crossing event between the child layers do not need to trigger the function.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
你的javascript有mouseenter,但你的标题和描述正在谈论mousemove..所以我假设你的意思是mousemove。处理此问题的一种方法是仅当父级具有特定类时才将函数绑定到 mousemouse 事件。与“active”类似,当鼠标移到子级上时,从父级中删除活动类。
这是一个要测试的小提琴
Your javascript has mouseenter, but you're title and description is talking about mousemove..so i assume you meant mousemove. One way you can handle this is to bind the function to a mousemouse event only if the parent has a certain class. Like 'active' and remove the class active from the parent when the mouse goes over the child.
Here is a fiddle to test
您需要阻止孩子的事件传播回父母。
You need to block the events from the children from propagating back to the parents.