简体   繁体   中英

How to NOT trigger mouseleave on parent when mouseenter on child element?

Haven't been able to get this work. Without going to my exact situation and why I'm trying to do what I'm doing, a basic abstract is:

I have 4 elements child elements inside a parent . I currently am attaching a mouseenter to the children so:

$('.parent').on('mouseenter', '.child', function() {
    showSomething();
});

Then I'm trying to say

$('.parent').on('mouseleave', function() {
    hideSameSomething();
});

But when the mouseenter on the child fires a mouseleave on the parent and creates a nasty flicker. I've tried e.stopPropagation() on the child handler but no luck. Any ideas how to not fire the parent mouseleave event when I'm entering the child elements and only when I really leave the parent?

Try combining both into one.

Official Document : http://api.jquery.com/on/#on-events-selector-data

$(".parent, .child").on({
    mouseenter: function() {
        // Handle mouseenter...
    },
    mouseleave: function() {
        // Handle mouseleave...
    }
});

Try:

$('.parent .child').on("mouseenter", function() {

    showSomething();

    $(this).closest('.parent').on("mouseleave", function() {
        hideSameSomething();
    });

});

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM