简体   繁体   English

在窗口调整大小时动态更改div jquery的高度

[英]dynamically change height of div jquery on window resize

I am using the code from .each() in a div element, find a child element, set height based on its content (advanced?) its working fine on page load but not working with window resize please help 在div元素中使用.each()中的代码,找到一个子元素,根据其内容设置高度(高级?),它在页面加载时工作正常,但无法使用窗口调整大小,请帮忙

$('#col-main > div').each(function () {
    var tmpHeight = 0;
    $(this).find("h2").each(function() {
        tmpHeight = $(this).height() > tmpHeight ? $(this).height() : tmpHeight;
    });
    $(this).find("h2").height(tmpHeight);
});

Adding working code on page load from this code I get height of taller div (column) ".demographcont" in a row (".box") which i set to other divs in the row for same height ".demographcont". 从此代码在页面加载上添加工作代码,我得到一行(“ .box”)中较高的div(列)“。demographcont”的高度,我将该行设置为同一高度“ .demographcont”中的其他div。 its worked for all rows. 它适用于所有行。 正常尺寸

Not worked on window resize its reset the height and the height will come as per content 不适用于窗口大小调整,其重置高度和高度将根据内容显示 调整窗口大小

after page refresh on window resize 页面刷新后调整窗口大小 在此处输入图片说明

/* code for adding height to div as per content */
    function setHeight() {
        $('.box').each(function () {
            var tmpHeight = 0;
            $(this).find(".demographcont").each(function () {
                tmpHeight = $(this).height() > tmpHeight ? $(this).height() : tmpHeight;
            });
            $(this).find(".demographcont").height(tmpHeight);
            // alert(tmpHeight);
        });
    }

    $(document).ready(function () {
        setHeight();
    });

    $(window).resize(function () {
        setHeight();
    });

Html code here HTML代码在这里

<div class="box clearfix">
    <div class="demographcont clearfix">
        <div class="grid_12">
            <div class="grid_5">
                <label>
                    Date of Birth:</label>
            </div>
            <div class="grid_7">
                18/06/2013
            </div>
        </div>
        <div class="grid_12">
            <p>
                content1</p>
        </div>
    </div>
    <div class="demographcont">
        <div class="grid_12">
            <p>
                content1</p>
            <p>
                content1</p>
            <p>
                content1</p>
            <p>
                content1</p>
        </div>
        <div class="grid_12">
            <p>
                content1</p>
            <p>
                content1</p>
            <p>
                content1</p>
            <p>
                content1</p>
        </div>
    </div>
</div>

.demographcont{ border:1px solid #006599; .demographcont {边框:1px实线#006599; padding:0; 填充:0; line-height:20px; 行高:20像素; } }

OK this is what I got from your question : 好的,这是我从您的问题中得到的:

You want to find maximum height of div.demographcont elements and set this value for the rest of elements .After page load we will have some thing like this 您想找到div.demographcont元素的最大高度并为其余元素设置此值。在页面加载后,我们将得到如下内容

<div class="box clearfix">
    <div class="demographcont" style="height:304px">...</div>
    <div class="demographcont" style="height:304px">...</div>
</div>

Now again on window resize you want to do the same thing but as I told you in comment it's not possible because you already set the same height inline.Otherwise I'm not getting your question. 现在再次在调整窗口大小上您想做同样的事情,但是正如我在评论中告诉您的那样,这是不可能的,因为您已经设置了相同的内联高度。否则我不会回答您的问题。

So try adding auto height property then doing the rest of proccess: 因此,请尝试添加自动高度属性,然后执行其余的过程:

 function setHeight() {
    $('.box').each(function () {
        var tmpHeight = 0;
        $(this).find(".demographcont").each(function () {
        $(this).height('auto');
            tmpHeight = $(this).height() > tmpHeight ? $(this).height() : tmpHeight;
        });
        $(this).find(".demographcont").height(tmpHeight);
        // alert(tmpHeight);
    });
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM