Accessing DOM object after AJAX call?

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

    how about:

    $.ajax({
       url: url,
       success: function(data) {
          $('body').append(data).find('#new_div').show();
       }
    });
    
    0 讨论(0)
  • 2020-12-29 15:31

    Actually this sort of things can be solved by following way: (I know it is similar to others, but a little bit more clear)

    $.ajax({
       url: url,
       success: function(data) {
          $('body').append(data);
          afterHtmlAppendCallback();
       }
    });
    function afterHtmlAppendCallback()
    {
        $('#new_div').show();
    }
    
    0 讨论(0)
  • 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!")
    });
    
    0 讨论(0)
  • 2020-12-29 15:40

    If you want to decouple your code from the callback:

    functionWithALotOfStuffToDo = function(data){
      // do stuff here
    }
    
    $.ajax({
       url: url,
       success: functionWithALotOfStuffToDo
    });
    
    0 讨论(0)
  • 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

    0 讨论(0)
  • 2020-12-29 15:44

    Assuming the data being returned is something like <div id='new_div' /> then try something such as

    var newDiv = null;
    
    $.ajax({
        url: url,
        success: function(data) {
            newDiv = $(data).appendTo($('body'));
        }
    });
    

    This will add the <div /> to the body of your page, and assign the jQuery element to the variable newDiv which can then be accessed again at a later stage.

    However, if you access newDiv before success has been returned, it will be null or the previous value, if it was assigned previously.

    0 讨论(0)
提交回复
热议问题