click() jquery function not available on new divs

前端 未结 4 730
说谎
说谎 2021-01-19 05:44

During the running of my website i create new divs using jquery with class \"a\". I have defined a click() function for \"a\" class as follows:

$(document).r         


        
相关标签:
4条回答
  • 2021-01-19 05:58

    Use the "live" method.

    http://api.jquery.com/live/

    0 讨论(0)
  • 2021-01-19 06:02

    Try using the .live() function which should also apply for newly inserted DOM elements that match your selector:

    $(document).ready(function() {
        $(".a").live('click', function(){
            $(".a").hide();
        });
    });
    
    0 讨论(0)
  • 2021-01-19 06:13

    This might also work.

    (function($) {  
      $(document).ready(function () {
        $('body').on('click', '.a', function() {
          $(this).hide();
        });
      });
    }( jQuery ));
    

    .on() attaches events to controls that are both present and not yet present.

    0 讨论(0)
  • 2021-01-19 06:14
    $(document).delegate('.a', 'click', function() {
      $(this).hide();
    });
    
    0 讨论(0)
提交回复
热议问题