0

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

我应该如何使用 jquery 的实时功能模拟 mouseenter 事件?

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

2 回答 2

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

$('ul.cms_tabs_edit').delegate('li', 'mouseleave', function() {
    $(this).removeClass('hover');
});
于 2012-01-07T18:11:24.167 回答
0

我最终做了:

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

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

于 2010-12-05T00:21:28.720 回答