My approach at the time was to bind the event handler manually when I added it. But the new version of jquery has added this functionality. We don't have to worry about it anymore.
Reference: http://api.jquery.com/live/
Previously we defined events such as the Click event for an element definition to be written like this:
Copy Code code as follows:
$ (' input '). Click (function () {
Handling code
});
Or
Copy Code code as follows:
$ ('. ClickMe '). Bind (' click ', function () {
Bound handler called.
});
However, this can only be defined for an element that has already been loaded, and those elements that are added later will need to be bound separately. Even if you use jquery's clone function, it does not replicate events (so far I'm not sure why it is so defined, whether it can be copied or deliberately handled so as to prevent some exceptions, which has yet to be analyzed for jquery's source code).
Now, with live you can easily handle,
$ ('. ClickMe '). Live (' click ', function () {//Live handler called.}); In this way, even the elements that you insert dynamically later will be bound to the event, $ (' body '). Append (' <div class= ' ClickMe ">another target</div> ');