Here said
$("table").delegate("td", "hover", function(){
$(this).toggleClass("hover");
});
is same to
$("table").each(function(){
$("td", this).live("hover", function(){
$(this).toggleClass("hover");
});
});
But I think it is also same to
$("table td").live("hover", function(){
$(this).toggleClass("hover");
});
Thus the method delegate is waste, right?
The biggest difference (I think) is that
live()attaches the event handler to thewindowelement. Whereasdelegate()binds the handler to the element you apply the method on.So in theory,
delegate()should be faster, especially for deeper nested structures, as the event does not have to bubble up the whole DOM tree.Now since jQuery 1.4, if you use
live(), the handler is bound to the context node, which iswindowby default. Thus(context node explicitly set to
thiswhich refers totable) is not equal toIn the latter example, the handler will be bound to
window.So
delegate()achieves the same effect as the first example with much simpler code and is therefore easier to understand imo.