如何使用jQuery在文档中的一个类除了事件处理程序上的文档?

时间:2012-10-02 11:06:54

标签: javascript jquery mouseevent

我需要在mousemove事件中获取整个文档,除了使用jQuery的文档中的一个类

我的班级是no-mousemove-node

$(document).mousemove(
        function(e){  
        ....
});.

我尝试过如下,但没有工作

$(document).not('.no-mousemove-node').mousemove(
        function(e){  
        ....
});.

是否可以这样做?

1 个答案:

答案 0 :(得分:2)

在事件处理程序中使用e.target查看源元素是否符合您的条件,如果不符合则中止执行:

$(document).mousemove(
    function(e){  
        if ($(e.target).is(".no-mousemove-node")) return;
        // now do what you need
    }
});

更新:如果您还需要过滤掉.no-mousemove-node使用.closest的后代,以确定您是父母还是父母.no-mousemove-node }:

function(e){  
    if ($(e.target).closest(".no-mousemove-node").length) return;
}