.append之后的jQuery函数 [英] jQuery function after .append

查看:77
本文介绍了.append之后的jQuery函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在jQuery .append完全完成后如何调用函数?

How to call a function after jQuery .append is completely done?

这是一个例子:

$("#root").append(child, function(){
   // Action after append is completly done
});

问题:当附加复杂的DOM结构时,在append函数回调中计算根元素的新大小是错误的.假设DOM仍未完全加载,只有添加的复杂DOM的第一个子对象.

The issue: When a complex DOM structure is appended, calculation of new size of the root element within the append function callback is wrong. Assumptions are that the DOM in still not completely loaded, only the first child of the added complex DOM is.

推荐答案

您在这里有很多有效的答案,但没有一个能真正告诉您它为什么如此有效的答案.

You've got many valid answers in here but none of them really tells you why it works as it does.

在JavaScript中,除非您通过使用超时或间隔明确告诉它们是异步的,否则一次只能一次执行命令,并按出现的顺序同步执行.

In JavaScript commands are executed one at a time, synchronously in the order they come, unless you explicitly tell them to be asynchronous by using a timeout or interval.

这意味着您的.append方法将被执行,并且在该方法完成其工作之前,将不执行任何其他操作(不考虑任何潜在的超时或间隔).

This means that your .append method will be executed and nothing else (disregarding any potential timeouts or intervals that may exist) will execute until that method have finished its job.

总而言之,由于.append将同步运行,因此无需回调.

To summarize, there's no need for a callback since .append will be run synchronously.

这篇关于.append之后的jQuery函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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