I want to use jQuery's great live functionality for a mouseenter event, but it's currently unsupported. Below is my first go at a solution, but it doesn't seem optimal. Suggestions? Improvements?
// mouseenter emulation
jQuery('.selector').live('mouseover',function (e) {
// live sees all mouseover events within the selector
// only concerned about events where the selector is the target
if (this != e.target) return;
// examine relatedTarget's parents to see if target is a parent.
// if target is a parent, we're "leaving" not entering
var entering = true;
jQuery(e.relatedTarget).parents().each(function () {
if (this == e.target) {
entering = false;
return false; // found; stop searching
}
});
if (!entering) return;
/*
the rest of my code
*/
});
I can't check target's children for relatedTarget b/c there's not an easy way to get all child nodes. I can't check directly if target's parents have relatedTarget as a parent and thus "entering" target, b/c for mouseover, it could be entering from an adjacent sibling and not a parent.
This solution works ok. I've tested it, and it seems fine, but how could I improve it? It also suffers from how the DOM is laid out. Some part of the selector elements must be exposed to see the mouseover event, but that's rarely a problem in the examples that I'm trying this on. Still, if there's a way to guarantee that it will be seen, that would be nice.
I guess I want to know if I'm approaching this right, and if not, what's better?