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 (thi开发者_如何学编程s != 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
*/
});
$('ul.cms_tabs_edit').delegate('li', 'mouseenter', function() {
$(this).addClass('hover');
});
$('ul.cms_tabs_edit').delegate('li', 'mouseleave', function() {
$(this).removeClass('hover');
});
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?
精彩评论