I am trying to detect whether a target element is either a child element, or the same element, that I have as an object reference.
I am using the following logic:
$("html").on("mousewheel.scroll DOMMouseScroll.scroll",function(e){
e.preventDefault();
var $scrollableElement = $(".foo").eq(0);
var $target = $(e.target);
if ($target == $scrollableElement
|| $target.parents($scrollableElement).length) {
alert("scroll");
}
});
However "scroll" is alerted even when scrolling over an element which is neither .foo
or a child of .foo
.
In my JsFiddle example, scroll over the .bar
element, and "scroll" is still alerted:
Why is this? Is it possible to use jQuery object references as the selector of .parents()
?