最佳答案
在 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>
CSS:
#parent
{
display: none;
}
Javascript:
var tableWidth = $('#parent').children('table').outerWidth();
if (tableWidth > $('#parent').width())
{
$('#parent').width(tableWidth);
}
TableWidth 总是返回0,因为它不可见(这是我的猜测,因为它在可见时给我一个数字)。有没有办法在不显示父表的情况下获得表的宽度?