What is the correct syntax for `.live ()` binding the 'hover' method?

We call the method .live()as follows:

$('.link').live('click', loadContent);

But what if I get attached to hoverinstead, which calls two functions separated by a comma? When I put in this:

$('.thumb.dim').live('hover', function(){$(this).removeClass('dim');}, function(){$(this).addClass('dim');});

The event mouseentercalls the first function above ( removeClass('dim')), but mouseleavenothing happens. Is there a proper way to write this?

+3
source share
3 answers

live performs only one callback, but you can check the passed event information for the callback type:

$('.thumb.dim').live('hover', function(e) {
    if (e.type == 'mouseover') {
        $(this).removeClass('dim');
    } else {
        $(this).addClass('dim');
    }
});

Alternatively, since you are just deleting / adding a class, you can do;

$('.thumb.dim').live('hover', function(e) {
    $(this).toggleClass('dim');
});
+2
source

- , jQuery .

, , ? dim_disabled , mouse-out dim_disabled, dim?

+2

Duh! The function removes the class that runs the function! When a class 'dim'is deleted on mouseenter, it thisno longer responds to the event mouseout!

0
source

Source: https://habr.com/ru/post/1758688/


All Articles