Accessing DOM object after AJAX call?

后端 未结 6 1891
南方客
南方客 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

    I think it's ajax async cause the problem you mention.

    In jQuery ajax funciton API says: Perform an asynchronous HTTP (Ajax) request.

    If you want to access the data from ajax right after request

    you should put you code in the ajax.success function like:

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

    Or turn the async setting into false

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

    that will make sure the $('#new_div') selector gets the object

提交回复
热议问题