JQuery `on` and related selectors [Body vs Exact Element Selection]

What is the significant difference between the next two?

$('body').on('click','#selector', function{...})
$('#selector').on('click', function{...})

I noticed that the first one more dynamic thing. If I have static selectors, which should I use? What is the performance? Which is preferable?

And it is: always use delegation? What are the side effects?

The answer here explains the use of both: Direct vs. Delegated - jQuery.on () . I wanted to see performance and best practices .

+4
source share
3 answers

This

$('body').on('click','#selector', function{...})

body, , , DOM , , ( , #selector, ).

:

$('#selector').on('click', function{...})

, , DOM.

- ( , ).

, - , ( , 1), ( , ).

+5

, , , tbody, ( tr tbody):

:

$( "#dataTable tbody" ).on( "click", "tr", function() {
  console.log( $( this ).text() );
});

: http://api.jquery.com/on/

0

- .

click body , , #selector. , DOM .

#selector. , #selector DOM, , .

0

All Articles