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

标签 jquery 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");
})

如何只在父级上绑定(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/

相关文章:

javascript - 多选字段(Jquery Select2 插件)

c++ - Windows - 在 C++ 中读取鼠标的 dpi 设置

jquery - 将鼠标光标移动到元素上

javascript - 当鼠标停止或改变方向时获取鼠标坐标

jquery - Twitter Bootstrap 远程模式每次都显示相同的内容

javascript - 为多个字段执行 keypress() 的另一种方法

javascript - 使用 AngularJS 将除所选单选按钮之外的值设置为 False

javascript - 浏览器在上传到服务器时找不到背景图像?

c# - 单击表单时移动表单,而不是顶栏区域

javascript - "touchmove"Android &iPhone 上使用 PhoneGap 的事件