简体   繁体   English

在AJAX调用之后访问DOM对象?

[英]Accessing DOM object after AJAX call?

I have a typical AJAX call that appends some HTML to the current page. 我有一个典型的AJAX调用,该调用将一些HTML追加到当前页面。 I want to be able to access the newly inserted HTML with typical jQuery selectors. 我希望能够使用典型的jQuery选择器访问新插入的HTML。

Here's what I'd like to be able to do... 这里是我能够做到......

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

$('#new_div').show();

#new_div would be some HTML element from the data I retrieved. #new_div将是我检索到的数据中的一些HTML元素。 I don't necessarily want to attach events to the new elements (like click ), so using something like .load() or .on() doesn't work here (as far as I know). 我不一定要附加事件到新的元素(如click ),因此使用类似.load().on()不在这里工作了(据我所知)。

I tried setting the $.ajax() call to a variable: var new_div = $.ajax(...) but that didn't get me anywhere. 我尝试将$.ajax()调用设置为变量: var new_div = $.ajax(...)但这并没有帮助我。

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: 如果您想在将新内容插入DOM之后(或什至之前)立即处理新内容,也可以将其放入AJAX成功回调中:

$.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: 另一方面,如果您要将处理程序绑定到将通过ajax添加到页面的内容,jQuery会这样做:

$(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!")
});

how about: 怎么样:

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

If you want to decouple your code from the callback: 如果要将代码与回调分离:

functionWithALotOfStuffToDo = function(data){
  // do stuff here
}

$.ajax({
   url: url,
   success: functionWithALotOfStuffToDo
});

Assuming the data being returned is something like <div id='new_div' /> then try something such as 假设返回的数据类似<div id='new_div' />然后尝试类似

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. 这会将<div />添加到页面的正文中,并将jQuery元素分配给变量newDiv ,然后可以在以后的阶段再次访问它。

However, if you access newDiv before success has been returned, it will be null or the previous value, if it was assigned previously. 但是,如果在返回success之前访问newDiv ,则它将为null或以前的值(如果先前已分配)。

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();
}

I think it's ajax async cause the problem you mention. 我认为这是ajax异步导致您提到的问题。

In jQuery ajax funciton API says: Perform an asynchronous HTTP (Ajax) request. 在jQuery中,ajax funciton API表示:执行异步HTTP(Ajax)请求。

If you want to access the data from ajax right after request 如果要在请求后立即从ajax访问数据

you should put you code in the ajax.success function like: 您应该将代码放在ajax.success函数中,例如:

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

Or turn the async setting into false 或者将异步设置设为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 这将确保$('#new_div')选择器获取对象

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM