将 div 高度更改为 jQuery

Change div height with jQuery

我正在尝试动态更改 div 高度,这取决于 table 高度。 这是我的代码:

<div>
    <table class="responsive">
        <tbody>
            <tr>
                <th>Ref</th>
                <th>Version</th>
            </tr>
            <tr>
                <td>test</td>
                <td>2</td>
            </tr>
        </tbody>
    </table>
</div>
<div class="pinned">
    <table class="">
        <tbody>
            <tr>
                <th>Ref</th>
                <th style="display: none;">Version</th>
            </tr>

            <tr>
                <td>test</td>
                <td style="display: none;">2</td>
            </tr>
        </tbody>
    </table>
</div>

我希望 .pinned.responsive 的大小相同。或者 .pinned th.responsive th .

大小相同

我试过了:

hauteur = jQuery('.responsive').height();
jQuery('.pinned').css('height', hauteur);

如果我添加 ID 就可以工作,但是 tables 是动态生成的,所以...我想尝试不使用 ID。

我该怎么办?

像这样?您需要添加 "px" 作为 $JqueryKing 提到的。

$('.pinned').each(function(){
    $(this).css('height', $('.responsive').height()+"px");
});
$('.pinned th').each(function(){
    $(this).css('height', $('.responsive th').height()+"px");
});

如果要添加这些 "on the go" 您必须将其放入一个函数中,然后 运行 每 x 毫秒添加一次。

以下代码应该有效:

jQuery('.pinned').height(jQuery('.responsive').height());