jQuery - 不可見(jiàn)時(shí)獲取元素寬度(顯示:無(wú))似乎在jQuery中,當(dāng)元素不可見(jiàn)時(shí),width()返回0.有道理,但我需要獲取表的寬度,以便在顯示父級(jí)之前設(shè)置父級(jí)的寬度。如下所述,父母中有文本,使父母傾斜并且看起來(lái)很討厭。我希望父級(jí)只有表格的寬度并且包含文本。<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,因?yàn)樗豢梢?jiàn)(我猜是因?yàn)樗诳梢?jiàn)時(shí)給了我一個(gè)數(shù)字)。有沒(méi)有辦法獲得表格的寬度而不使父母可見(jiàn)?
jQuery - 不可見(jiàn)時(shí)獲取元素寬度(顯示:無(wú))
明月笑刀無(wú)情
2019-08-02 14:29:14