This article mainly introduces the usage and differences of delegate and on in jQuery in detail. If you need a friend, you can refer to it for help.
In jQuery1.7,. delegate () has been replaced by. on. For earlier versions, it still uses the most effective means of event delegation. In event binding and delegation, the delegate () and on methods are generally equivalent. Add one or more event handlers to the element specified by. delegate (), which belongs to the child element of the selected element, and specify the function to run when these events occur. The Code is as follows: // jQuery 1.4.3 + $ (elements ). delegate (selector, events, data, handler); // jQuery 1.7 + $ (elements ). on (events, [selector], data, handler); example :. delegate () code: the code is as follows: $ ("table "). delegate ("td", "click", function () {alert ("hello ");});. on () code: the code is as follows: $ ("table "). on ("click", "td", function () {alert ("hi") ;}); PS: the difference between the two is that the subelement of the selected element in different order of seleter and events must be a "legal" subelement. For example, copy the Code as follows: $ ("table "). delegate ("button", "click", function (){...}); $ ("table "). on ("click", "p", function (){...}); it does not work, because under normal circumstances, the table sub-element should be tr, td... on (events, [selector], [data], fn), the [selector] parameter is optional. A selector string is used to filter the descendant of the selector element used to trigger events. For example, the Code is as follows: $ ("table "). on ("click ",". td1 ", function () {alert (" hi ") ;}); filters table sub-elements whose class is td1 and selector of delegate is required.