本文介绍了请问jQuery的追加()的行为异步?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经看到了一些职位(<一href=\"http://stackoverflow.com/questions/1539841/wait-untill-$p$pvious-append-is-complete-jquery\">append按说立即)与冲突的接受了这个答案。我们使用JQuery 1.4 (http://ajax.googleapis.com/ajax/libs/jqueryui/1.8/jquery-ui.min.js)和append()似乎是异步的,这样的即:

I've seen a number of posts (append supposedly immediate) with conflicting accepted answers on this. We're using JQuery 1.4 (http://ajax.googleapis.com/ajax/libs/jqueryui/1.8/jquery-ui.min.js) and append() seems to be asynchronous, such that:

编辑,以显示code在AJAX回调的背景下

Edited to show code in context of AJAX callback

 ...
 var message = $.ajax({
   type: "GET",
   url: "/getVolumes/" +  _Id,
   async: false
 }).responseText;
 if (parseInt(message) != 0){
   var $results = $(message);
   $MAIN_DIV.append($results);
   retrieveTargets();
 }
...
function retrieveTargets(){
  var $targets = $(".resultTargets");
}

执行并创建页面如预期,但该指标的查询产生什么在运行。在JS控制台运行相同的code如预期检索的元素。

Executes and creates the page as expected, yet the targets query yields nothing at runtime. Running the same code in the JS console retrieves the elements as expected.

如果这是JQuery的预期的行为有什么正确的方法要等到追加完成?

If this is the expected behavior in JQuery what's the proper way to wait until append is finished?

推荐答案

所有的意见,帮助追踪下来。我跟随在控制台中的红鲱鱼。
问题是不同步性,这是下一个行:

All of the comments helped track this down. I was following a red herring in the console.The problem wasn't with synchronicity, it was with the next lines:

$targets.each( function(){
  ...
  this.html();
  ...

需要的是

$(this).html();

总之,大家是正确的。
jQuery的追加()的行为同步。

这篇关于请问jQuery的追加()的行为异步?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-20 16:51