2011-05-03 35 views
8

难道一个表行(<tr>)必须在表体(<tbody>),如果表中有一个表体,也可以存在于表体的外面?难道一个<tr>必须是内<tbody>

<table> 
    <tr> 
     <td colspan='2'>...</td> 
    </tr> 

    <tbody> 
     <tr> 
     <td>...</td> 
     <td>...</td> 
     </tr> 
    </tbody> 

    <tr> 
     <td colspan='2'>...</td> 
    </tr> 

    <tbody> 
     <tr> 
     <td>...</td> 
     <td>...</td> 
     </tr> 
    </tbody> 

</table> 
+1

你试过上面的代码? http://jsfiddle.net/wQpfb/ – Dutchie432 2011-05-03 16:55:30

+0

上面的代码工作,我想知道,如果它是有效的,根据w3 – superUntitled 2011-05-03 16:56:32

+1

http://validator.w3。org/ – Dutchie432 2011-05-03 17:01:40

回答

5

没有,<tr>可以在<thead><tbody><tfoot>也不必在任何人。

+6

虽然它可以是任何'thead','tbody'或'tfoot'元素,如果'tr'直接在'table'中找到,浏览器将*添加一个'tbody'来包含'tr's。所以,它有点* *。 – 2011-05-03 16:58:13

+0

将添加的''显示在所呈现的html中,还是动态地完成某些操作而不改变代码? – 2011-05-03 17:06:49

+0

在Chromium(Ubuntu 11.04)中,动态添加了一个“tbody”元素(在Web Inspector中可见,但在框架的源中不可见)。演示[在JS小提琴](http://jsfiddle.net/davidThomas/UMMVS/)。 – 2011-05-03 17:13:37

1

<tbody>用来标记您的<table>的身体,如果你的表中包含<thead>(表头)。 。或<tfoot>(表尾)元素如果您的表不包含这些元素,你可以自由地不使用<tbody>

正确用法是:

<table> 
<thead><tr><th>Item   </th><th>Cost </th></tr></thead> 
<tbody><tr><td>Stack Overflow</td><td>Free </td></tr> 
     <tr><td>Something cool</td><td>$1.00</td></tr></tbody> 
</table> 

HTML4 specification to tables

1

如果你有<tr><tbody>的,该网页将无法验证: http://validator.w3.org

正如其他人指出,<tbody>是可选的,除非你使用<thead><tfoot>。使用后两个元素的主要原因是,如果打印一张长桌子,页眉和页脚会在每个页面上重复。

这听起来像你可能会创建类似日历的东西,你想要交替行<th>(例如,日期)和<td>(例如,该日期的事件)。如果是这样的话,你不应该在<thead><tbody>中包裹交替行 - 这样做会在打印页面时混淆浏览器。如果你只留下分组元素,你的表格将被验证。但是,某些屏幕阅读器可能会被该标记混淆,并将最上面的一行标题应用于它们下面的所有单元格。对于像这样的复杂表格,您需要添加额外的标记以确保屏幕阅读器了解表格的组织方式。下面是与访问标记你的表:

<table summary="A brief description of how the table is organized, for screen reader users"> 
    <tr> 
    <th colspan='2' id="header1">...</th> 
    </tr> 
    <tr> 
    <td headers="header1">...</td> 
    <td headers="header1">...</td> 
    </tr> 
    <tr> 
    <th colspan='2' id="header2">...</th> 
    </tr> 
    <tr> 
    <td headers="header2">...</td> 
    <td headers="header2">...</td> 
    </tr> 
</table> 

或者,你可能要考虑数据是否可以在多个表组织,或者如果可以提供另一种版本,这将是更容易地使用屏幕阅读器的用户。例如,事件日历可以另外呈现为事件列表。

6

相反的是特里尔汤姆森表示,与<thead><tfoot><tbody>标签之外,但在<table>标签内<tr>标签表将验证对W3C Markup Validation Service

该文件被成功选中为HTML 4.01过渡:

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> 
<html lang="en"> 
    <head> 
    </head> 
    <body> 
    <table> 
     <thead> 
     <th colspan="2">head1</th> 
     <th>head2</th> 
     </thead> 
     <tfoot> 
     <th colspan="2">foot1</th> 
     <th>foot2</th> 
     </tfoot> 

     <tr><td colspan="3">this row is outside of thead and tfoot</td></tr> 

     <tbody> 
     <tr> 
      <td>1-1</td> 
      <td>1-2</td> 
      <td>1-3</td> 
     </tr> 
     <tr> 
      <td>2-1</td> 
      <td>2-2</td> 
      <td>3-3</td> 
     </tr> 
     <tr> 
      <td>3-1</td> 
      <td>3-2</td> 
      <td>3-3</td> 
     </tr> 
     </tbody> 
    </body> 
</html> 
相关问题