jquery - 加载 div 内的所有图像后执行函数

标签 jquery load

我想用 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/

相关文章:

javascript - 我如何处理多个列表项被放入一个临时占位符,并设置回它被删除的地方?

javascript - 页面完全加载时窗口滚动位置

java - Struts2 + Spring4 + Hibernate3 分页

php - 编写托管在负载平衡服务器中的 Web 应用程序时需要了解哪些事项?

jquery - 如何格式化使用 $.load 或 $.get 或 $.ajax 加载的(css)数据?

javascript - ajax post 不适用于 ajax get from inside div

r - 加载别人的.rdata文件,无法访问数据

java - SharedPreferences 不保留值(value)

javascript - 如何在 JavaScript 中为复杂对象上的图像设置加载事件

jquery - $().html 附加到影子 DOM 而不是替换