Which of the following ways is more efficient? What is the difference?
A
$("body").on('click', '.picker', function(){ alert("A");});
B
$('.picker').on('click', function(){ alert("B");});
Which of the following ways is more efficient? What is the difference?
A
$("body").on('click', '.picker', function(){ alert("A");});
B
$('.picker').on('click', function(){ alert("B");});
Depends on the situation.
A
advantages
.picker
could be more than just a few. Delegating the handler to a parent, and not attaching a handler to each .picker
reduces overhead..picker
is an element that is loaded dynamically, but you need to set handlers ahead of time.B
advantages
In a similar context, .live
was deprecated because handlers were placed on document
, the highest node in the DOM tree. It caused latency.