2011-01-29 74 views
0

認爲我有這樣的代碼:我可以強迫tr不要崩潰嗎?

 

<table> 
    <tr style="background-color: red"><td><pre>This is line one</pre></td></tr> 
    <tr style="background-color: red"><td><pre></pre></td></tr> 
    <tr style="background-color: red"><td><pre>This is line three</pre></td></tr> 
    <tr style="background-color: red"><td><pre>This is line four</pre></td></tr> 
</table> 
 

我想是第二行是相同的高度其它線路。有沒有辦法做到這一點?

回答

0

HTML解決方案:

變化

<tr style="background-color: red"><td></td></tr> 

<tr style="background-color: red"><td>&nbsp;</td></tr> 

但你真的應該用C SS類來設置行的樣式,而不是使用「樣式」屬性。

1

設置一個樣式。

tr{ 
    height: 20px; 
} 

如果你想使用JavaScript(jQuery),那麼這樣的事情。

var maxHeight =0; 
$("tr").each(function(){ 
    if($(this).height()>maxHeight){ 
     maxHeight = $(this).height(); 
    } 
}); 

$("tr").css({height: maxHeight+'px'}); 

這裏是一個的jsfiddle:http://jsfiddle.net/Xtyqr/

+0

我怎麼會知道該行的高度? – 2011-01-29 22:05:30

+0

你將不得不選擇一個字體大小。然後使用像螢火蟲一樣的工具來確定高度。如果你想動態地做到這一點,你將需要使用一些JavaScript。 – rcravens 2011-01-29 22:07:28

+0

rcravens是對的。另外 - 給表一個標識符/類,並將該「背景」內聯樣式標籤移動到CSS文件。 HTML僅用於內容。 – singles 2011-01-29 22:36:10

0

rcravens是對的。

如果你願意,你也可以直接設置高度在HTML:

<table> 
<tr style="background-color: red" height = 20><td>This is line one</td></tr> 
<tr style="background-color: red" height = 20><td></td></tr> 
<tr style="background-color: red" height = 20><td>This is line three</td></tr> 
<tr style="background-color: red" height = 20><td>This is line four</td></tr> 
</table> 

在Firefox 4測試版10