How to get height of the highest children element in javascript/jQuery?

后端 未结 4 633
無奈伤痛
無奈伤痛 2020-12-05 05:10

I need a function to get the height of the highest element inside a div.

I have an element with many others inside (dynamically generated), and when I ask for the he

4条回答
  •  日久生厌
    2020-12-05 05:43

    In my case I had to use it with a responsive design so I made it work with window resize.

    function fixHeight(elem){
        var maxHeight = 0;
        $(elem).css('height','auto');
        $(elem).each(function(){
           if ($(this).height() > maxHeight) { maxHeight = $(this).height(); }
        });
        $(elem).height(maxHeight);
    }
    
    $(window).resize(function () {
        fixHeight('.myelement');
    });
    $(document).ready(function(e) {
        fixHeight('.myelement');
    });
    

    I hope this will help someone !

    Happy coding guys ! :)

提交回复
热议问题