2011-01-29 77 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