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
Release: 2016-05-16 16:10:40
Original
1091 people have browsed it

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>
Copy after login

You need to add a click event to the

  • tag dynamically added inside


      Before jquery version 1.7, live dynamic binding events were used

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

      After jquery version 1.7, use on to dynamically bind events

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

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

  • 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