【发布时间】:2010-12-01 03:12:33
【问题描述】:
似乎在 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,因为它不可见(这是我的猜测,因为它在可见时给了我一个数字)。有没有办法在不使父级可见的情况下获得表格的宽度?
【问题讨论】: