相关疑难解决方法(0)

jQuery - 不可见时获取元素宽度(显示:无)

似乎在jQuery中,当元素不可见时,width()返回0.有道理,但我需要获取表的宽度,以便在显示父级之前设置父级的宽度.

如下所述,父母中有文本,使父母倾斜并且看起来很讨厌.我希望父级只有表格的宽度并且包含文本.

<div id="parent">
    Text here ... Can get very long and skew the parent
    <table> ... </table>
    Text here too ... which is why I want to shrink the parent based on the table
</div>
Run Code Online (Sandbox Code Playgroud)

CSS:

#parent
{
    display: none;
}
Run Code Online (Sandbox Code Playgroud)

使用Javascript:

var tableWidth = $('#parent').children('table').outerWidth();
if (tableWidth > $('#parent').width())
{
    $('#parent').width(tableWidth);
}
Run Code Online (Sandbox Code Playgroud)

tableWidth总是返回0,因为它不可见(我猜是因为它在可见时给了我一个数字).有没有办法获得表格的宽度而不使父母可见?

jquery width

107
推荐指数
3
解决办法
12万
查看次数

标签 统计

jquery ×1

width ×1