In jQuery1.7 .delegate() has been replaced by .on(). As with earlier versions, it still uses the most efficient means of event delegation.
In event binding and delegation, delegate() and on are generally equivalent.
.delegate() adds one or more event handlers to the specified element (a child element of the selected element) and specifies the function to run when these events occur.
// jQuery 1.4.3
$( elements ).delegate( selector, events, data, handler );
// jQuery 1.7
$( elements ).on( events, [selector], data, handler );
For example: .delegate() code:
$("table ").delegate("td","click",function(){
alert("hello");
});
.on() code:
$("table").on("click", " td", function() {
alert("hi");
});
PS: The difference between the two is that the order of selector and events is different
delegate and on method The child elements of the selected element must be "legal" child elements. For example,
$("table").delegate("button ","click",function(){...});
$("table").on("click", "p", function(){...});
will not work, because under normal circumstances, table sub-elements should be tr, td...
on(events,[selector],[data],fn), parameter [selector ] is optional,
A selector string that is used as a descendant of the selector element that triggers the filter event.
For example:
$("table"). on("click", ".td1", function() {
alert("hi");
});
Filter table sub-elements with class td1
The delegate’s selector is required.