Accessing DOM object after AJAX call?

后端 未结 6 1904
南方客
南方客 2020-12-29 14:57

I have a typical AJAX call that appends some HTML to the current page. I want to be able to access the newly inserted HTML with typical jQuery selectors.

Here\'s wha

6条回答
  •  北荒
    北荒 (楼主)
    2020-12-29 15:40

    If you would like to manipulate the new content immediately after (or even before) inserting it to the DOM, you can put that in the AJAX success callback too:

    $.ajax({
       url: url,
       success: function(data) {
          $('body').append(data);
          $('#new_div').show();
       }
    });
    

    On the other hand, if you want to bind handlers to content that will be added to the page via ajax, jQuery does that like this:

    $(document).on('click', '#new_div', function(){
      alert("This function is bound to all #new_div's click events, even if they are added to the DOM via ajax later!")
    });
    

提交回复
热议问题