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 .
This
$('body').on('click','#selector', function{...})
body, , , DOM , , ( , #selector, ).
body
#selector
:
$('#selector').on('click', function{...})
, , DOM.
- ( , ).
, - , ( , 1), ( , ).
, , , tbody, ( tr tbody):
$( "#dataTable tbody" ).on( "click", "tr", function() { console.log( $( this ).text() ); });
: http://api.jquery.com/on/
- .
click body , , #selector. , DOM .
#selector. , #selector DOM, , .