Jquery append() 行为异步吗? [英] Does Jquery append() behave asynchronously?

查看:42
本文介绍了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:

编辑以在 AJAX 回调上下文中显示代码

 ...
 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 控制台中运行相同的代码会按预期检索元素.

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 append() 的行为是同步的.

In short, everyone was correct. Jquery append() behaves synchronously.

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

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆