2010-12-03 60 views
0

我想重新打开别人问的问题。使用live或delegate模拟mouseenter的最佳方式是什么?原来的问题在这里: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 
    */ 
}); 
+0

我不明白这个“问题”。 – jAndy 2010-12-03 09:51:47

回答

0

我落得这样做:

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

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

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

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