jquery - What's the difference between .delegate() and live()? -
since
$("table").delegate("td", "hover", function(){ $(this).toggleclass("hover"); }); is equivalent following code written using .live():
$("table").each(function(){ $("td", this).live("hover", function(){ $(this).toggleclass("hover"); }); }); according jquery api.
i bet i'm wrong isn't same of writing
$("table td").live('hover', function() {}); so, what's .delegate() for?
.live() listens on document .delegate() listens on more local element, <table> in case.
they both act same listening events bubble, 1 .delegate() bubbles less before being caught.
your example of:
$("table td").live('hover', function() {}); isn't same, again attaches event handler document , not <table>, .delegate() more local elements, more efficient in respects...though still uses .live() under covers.
also keep in mind $(selector) retrieves elements, $("table td") selects bunch of elements no reason when using .live(), $("table").delegate() selects only <table> elements, it's more efficient not running selector , discarding result.
Comments
Post a Comment