Home > Web Front-end > JS Tutorial > jQuery dynamically added element binding event handling function code_jquery

jQuery dynamically added element binding event handling function code_jquery

WBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWB
Release: 2016-05-16 18:03:59
Original
1164 people have browsed it

My method at the time was to manually bind the event handler function when adding it. However, the new version of jquery has added this feature. We no longer need to worry about this.
Reference: http://api.jquery.com/live/
We used to define events, such as defining a click event for an element like this:

Copy code The code is as follows:

$('input').click(function () {
//Processing code
}) ;

or
Copy code The code is as follows:

$ ('.clickme').bind('click', function() {
// Bound handler called.
});

But this can only be done for loaded Element definition events, those elements that are added later 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 is handled this way deliberately to prevent certain exceptions, which remains to be analyzed) Take a look at the source code of jquery).
Now, you can do it easily using live,
$('.clickme').live('click', function() { // Live handler called. }); In this way, you can dynamically insert it later The element will also be bound to the event, $('body').append('
Another target
');
Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template