Difference: on() adds one or more event handlers to the selected element and sub-elements; live() adds one or more event handlers to the selected element and specifies when these events occur function to run. The on() method has one more parameter than live(), which is used to specify event handlers that can only be added to specified child elements.
Related recommendations: "jQuery Video Tutorial"
jquery on() method
The on() method adds one or more event handlers on the selected element and sub-elements.
Since jQuery version 1.7, the on() method is the new replacement for the bind(), live(), and delegate() methods. This method brings a lot of convenience to the API and is recommended because it simplifies the jQuery code base.
jquery live() method
The live() method adds one or more event handlers to the selected element and specifies the functions to run when these events occur. .
The difference between on() and live() in jquery
1. Different functions
on(): in Adds one or more event handlers to the selected element and child elements. Event handlers added using the on() method apply to current and future elements (such as new elements created by scripts).
live(): Attach one or more event handlers to the selected element and specify functions to run when these events occur. Event handlers attached via the live() method apply to current and future elements that match the selector (such as new elements created by a script).
2. Different syntax
on():
$(selector).on(event,childSelector,data,function)
where childSelector is optional. Specifies that event handlers can only be added to specified child elements (and not the selector itself, such as the deprecated delegate() method).
live():
$(selector).live(event,data,function)
event is required. Specifies one or more events to attach to the element. Multiple events separated by spaces. Must be a valid event.
For more programming-related knowledge, please visit: Programming Learning! !
The above is the detailed content of What is the difference between on() and live() in jquery?. For more information, please follow other related articles on the PHP Chinese website!