我想用 jQuery 获取 div 的高度,在这个 div 中,有一些图像在加载时会改变这个 div 的大小:
但是使用下面的代码,它在加载图像之前给出了 div 的高度
$(document).ready(function() {
$(".link_module").each(
function () {
maxheight = Math.max(maxheight,$(this).height());
}
).height(maxheight);
});
我已将代码更改为此,但没有任何变化:
$(document).ready(function() {
$(".link_module img").load(
function (){
$(".link_module").each(
function () {
maxheight = Math.max(maxheight,$(this).height());
}
).height(maxheight);
}
)
});
任何人都可以帮助我更改代码以实现我的目标吗?
最佳答案
您可以使用$(window).load()
当文档及其资源完全加载时触发,包括所有对象和图像:
The load event fires at the end of the document loading process. At this point, all of the objects in the document are in the DOM, and all the images and sub-frames have finished loading.
$(window).on('load', function() {
$(".link_module").each(
function () {
maxheight = Math.max(maxheight,$(this).height());
}
).height(maxheight);
});
关于jquery - 加载 div 内的所有图像后执行函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11457520/