您必须stopPropagation()
用于防止事件跟踪。
function Infocus(e) {
e.stopPropagation();
// your code
}
function Outfocus(e) {
e.stopPropagation();
// your code
}
阅读.stopPropagation()
你可以这样做:(可能不满意)
$("#parent").live({
mouseenter: Infocus,
mouseleave: Outfocus
});
$("#children").live({
mouseenter: Infocus,
mouseleave: Outfocus
});
function Infocus(e) {
if(this.id == 'parent') {
$(this).css('background', 'yellow');
} else if(this.id == 'children') {
$(this).css('background', 'green');
$(this).parent().trigger('mouseleave')
}
}
function Outfocus(e) {
if(this.id == 'parent') {
$(this).css('background', 'transparent');
} else if(this.id == 'children') {
$(this).css('background', 'transparent');
$(this).parent().trigger('mouseenter')
}
}
演示