использование мыши в режиме реального времени или делегирование? - PullRequest
0 голосов
/ 03 декабря 2010

Я хочу снова открыть вопрос, заданный кем-то другим. Какой лучший способ эмулировать мышиный центр с помощью live или делегата? Оригинальный вопрос был здесь:

Как мне эмулировать событие mouseenter, используя живую функциональность jquery?

И предложение ФП было:

// 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 ]

2 голосов
/ 07 января 2012
$('ul.cms_tabs_edit').delegate('li', 'mouseenter', function() {
    $(this).addClass('hover');
});

$('ul.cms_tabs_edit').delegate('li', 'mouseleave', function() {
    $(this).removeClass('hover');
});
0 голосов
/ 05 декабря 2010

Я закончил тем, что сделал:

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

У кого-нибудь есть лучшее решение?

...