This article mainly introduces jQuery's method of binding events to dynamically added elements. it compares the method of event binding in jQuery with the differences between common versions of jQuery and binding, and provides some reference value, for more information about how jQuery binds events to dynamically added elements, see the following example. Share it with you for your reference. The specific analysis is as follows:
Bind or click is generally used to bind events in jquery, but this can only be used to define events for elements that have been loaded. the elements that are subsequently inserted need to be bound separately. Use live before version 1.7. However, we recommend that you use on later than version 1.8. This section describes how to bind events to dynamically added elements in jQuery.
In actual development, a trigger event is bound to the dynamically generated html element.
For example
To
Dynamically added
- Label add click event
Previously, jquery 1.7 used live to dynamically bind events.
$ ("# Testp ul li"). live ("click", function (){});
Use on to dynamically bind events after jquery 1.7
$ ("# Testp ul"). on ("click", "li", function () {// do something here });
I hope this article will help you with jQuery programming.