What is the difference between:
$(document).on('click', '.selector', function(){}) and
$('.selector').on('click', function(){}) What is the fundamental difference and does it affect the speed of work?
$(document).on('click', '.selector', function(){}) or $('.parentSelector').on('click', '.childSelector', function(){}) - delegation events
Should use:
The whole point is that one event is hung on the parent selector (or higher selector), which responds to pop-up events from the child elements.
In terms of performance, this method is better when the conditions described above are met.
But I do not recommend hanging on the document or on the body unless absolutely necessary, because it negatively affects the performance . Try to use a more direct parent selector.
$('.selector').on('click', function(){}) should be used in all other cases, since best performance is achieved.
Source: https://ru.stackoverflow.com/questions/525793/
All Articles
$(document), but$('body')- MasterAlex