Accessing DOM object after AJAX call?

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

    Assuming the data being returned is something like

    then try something such as

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

    This will add the

    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.

提交回复
热议问题