Search code examples
delegateslivejquerymouseenter

mouseenter using live or delegate?


I want to re-open a question someone else asked. What's the best way to emulate mouseenter with live or delegate? The original question was here:

How should I emulate a mouseenter event using jquery's live functionality?

And the OP's proposal was:

// 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 
     */
});

Solution

  • I ended up doing:

    $("#id").delegate(".selector", "mouseover", function(){
        if(!$(this).hasClass("bound")){                                                                                                          
            $(this).hover(function(){
                alert('entering');
            },
            function(){
                alert('leaving');
            }).mouseover().addClass("bound");
        }
    });
    

    Does anyone have a better solution?