jQuery: How to add event handler to dynamically added HTML elements?

前端 未结 4 962
心在旅途
心在旅途 2020-12-03 12:36

I have the following code:

$(document).ready(function({
    $(\".click\").click(function(){
        alert(\' The Button Was Clicked !\');
    });
}));

4条回答
  •  萌比男神i
    2020-12-03 13:34

    In reference to your code, the way to do it would be.

    $('.click').live('click', function(){
      ... do cool stuff here
    });
    

    Using the .live() function allows you to dynamically attach event handlers to DOM objects. Have fun!

提交回复
热议问题