Home >Web Front-end >JS Tutorial >jQuery's method of binding events to dynamically added elements_jquery

jQuery's method of binding events to dynamically added elements_jquery

WBOY
WBOYOriginal
2016-05-16 16:10:401101browse

The example in this article describes how jQuery binds events to dynamically added elements. Share it with everyone for your reference. The specific analysis is as follows:

Binding events in jquery generally use bind or click, but this can only define events for elements that have been loaded, and elements that are added and inserted later need to be bound separately. Use live before version 1.7. However, it is recommended to use on after version 1.8. Here is an introduction to how to bind events to dynamically added elements in jQuery
In actual development, you will encounter situations where you need to bind trigger events to dynamically generated html elements

For example

<div id="testdiv">
  <ul></ul>
</div>

You need to add a click event to the 25edfb22a4f469ecb59f1190150159c6 tag dynamically added inside ff6d136ddc5fdfeffaf53ff6ee95f185

Before jquery version 1.7, live dynamic binding events were used

$("#testdiv ul li").live("click",function(){
});

After jquery version 1.7, use on to dynamically bind events

$("#testdiv ul").on("click","li", function() {
     //do something here
 });

I hope this article will be helpful to everyone’s jQuery programming.

Statement:
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