使用live或delegate的mouseenter?

时间:2010-12-03 09:37:53

标签: delegates live jquery mouseenter

我想重新打开别人问的问题。使用live或delegate模拟mouseenter的最佳方法是什么?最初的问题在这里:

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

OP的提议是:

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

2 个答案:

答案 0 :(得分:2)

$('ul.cms_tabs_edit').delegate('li', 'mouseenter', function() {
    $(this).addClass('hover');
});

$('ul.cms_tabs_edit').delegate('li', 'mouseleave', function() {
    $(this).removeClass('hover');
});

答案 1 :(得分:0)

我最终做了:

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

有没有人有更好的解决方案?