How useful are delegates?

I have two handlers for the mouseover function, I would like to know what the difference is and how to use each of them in the most efficient way.

The code block is one:

$('.test div').mouseover(function() {
                    $(this).stop().animate({
                        left: '100px'
                    }, 550);
});

Code block two:

$('.test').delegate('div', 'mouseover', function() {
                    $(this).stop().animate({
                        left: '100px'
                    }, 550);
                }); 

Thanks in advance:)

+3
source share
4 answers

.delegate()attaches one event handler mouseoverper element .test, and not each .test div, and in the future <div>elements work that are added inside those .testelements that you named .delegate()on.

<div> .test ( AJAX, ), . , , .delegate(), , / .

+3

: http://api.jquery.com/delegate/

, "" , , . :


    $('.test div').live('mouseover', function() {
                        $(this).stop().animate({
                            left: '100px'
                        }, 550);

, div, , .test, . live $(

live() delegate() , , , , .

+2

Delegates attach one or more events for all elements matching the selector, now or in the future, to a specific set of root elements.

Taken from jQuery API

0
source

From what I know about the delegate, it is that it allows you to modify Event.target. So let me say that your Element is clicked, you can re-target the DIV.

0
source

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


All Articles