My handling method at that time was to manually bind the event handler function when adding it. However, this feature has been added to the new version of jquery. We no longer need to worry about this.
Reference: /live/
In the past, we defined events, such as defining click events for elements, and wrote this way:
$('input').click(function () {
//Processing code
});
or
$('.clickme').bind('click', function() {
// Bound handler called.
});
However, this can only define events for elements that have been loaded, and those elements that are later added and inserted need to be bound separately. Even if you use jquery's clone function, it cannot copy the event (so far I don't know why it is defined like this, whether it cannot be copied or deliberately handled this to prevent certain exceptions. This still needs to be analyzed for the source code of jquery).
Now, using live you can easily do it.
$('.clickme').live('click', function() { // Live handler called. }); In this way, even the elements you insert dynamically afterwards will be bound to events, $('body').append('<div class="clickme">Another target</div>');
Reference: /live/
In the past, we defined events, such as defining click events for elements, and wrote this way:
Copy the codeThe code is as follows:
$('input').click(function () {
//Processing code
});
or
Copy the codeThe code is as follows:
$('.clickme').bind('click', function() {
// Bound handler called.
});
However, this can only define events for elements that have been loaded, and those elements that are later added and inserted need to be bound separately. Even if you use jquery's clone function, it cannot copy the event (so far I don't know why it is defined like this, whether it cannot be copied or deliberately handled this to prevent certain exceptions. This still needs to be analyzed for the source code of jquery).
Now, using live you can easily do it.
$('.clickme').live('click', function() { // Live handler called. }); In this way, even the elements you insert dynamically afterwards will be bound to events, $('body').append('<div class="clickme">Another target</div>');