为什么只有在页面加载时才能正常工作?

Why is this only working correctly on page load?

本文关键字:常工作 工作 加载 为什么      更新时间:2023-09-26

我有一个程序

<script type="text/javascript">
    function rescaleStuff ( )
    {
        jQuery('.children-have-equal-height').each(function(){
            var children = jQuery(this).children();
            var numChildren = children.length;
            if (numChildren > 1) 
            {
                var firstChild = children.first();
                var maxHeight = firstChild.height();
                firstChild.siblings().each(function()
                { 
                    var thisHeight = jQuery(this).height(); 
                    if (thisHeight > maxHeight) 
                        maxHeight = thisHeight;
                });
                children.height(maxHeight);
            }
        });
    }

    jQuery(window).load(function() {
         rescaleStuff();
    });
    jQuery(window).resize(function()
    { 
         rescaleStuff();
    });
</script>

,它的目的是使类children-have-equal-height的所有子元素的高度等于最高的子元素的高度。为什么它只在页面加载时工作,然后子元素的height保持与页面加载时相同?

尝试在recalestuff的开头设置高度为" auto "

jQuery('.children-have-equal-height').children().height('auto')