Binding dynamically created elements in jQuery

后端 未结 5 1729
伪装坚强ぢ
伪装坚强ぢ 2020-12-11 09:15

The following code is fairly self explanatory, but I am running in to an issue binding the click event to an element that has been created.

You can see at line 25 I

5条回答
  •  青春惊慌失措
    2020-12-11 10:18

    Using the .live() method it will work with any elements you add to the DOM after its loaded.

    // Overlay click = trigger out
    $('#overlay').live('click', function () {
        $('#overlay').fadeOut(1000);
        $('.modal').fadeOut(200);
    });
    

    Another way to do it would be to bind it to click when it is added (inside the click handler for $('a[name=modal]').

    You should probably also change $('#overlay').fadeOut() to $(this).fadeOut().

提交回复
热议问题