使用rowspan的HTML表头

m4n*_*n07 6 html html-table

<html>
<body>

<TABLE border="1" ">

<CAPTION><EM>A test table with merged cells</EM></CAPTION>

<TR><TH rowspan="2"><TH colspan="2"> Average
    <TH rowspan="2">Red<BR>eyes </TH>
</TR>
<TR>
  <TH>height</TH><TH>weight</TH>
</TR>
<TR> 
  <TD>1.9<TD>0.003<TD>40%</TD>
</TR>
<TR> 
  <TD>1.7<TD>0.002<TD>43%</TD>
</TR>

</TABLE>
</body>
</html>
Run Code Online (Sandbox Code Playgroud)

我得到的输出头的第一个元素为空白

  A test table with merged cells
/-----------------------------------------\
|          |      Average      |   Red    |
|          |-------------------|  eyes    |
|          |  height |  weight |          |
|-----------------------------------------|
|  1.9     | 0.003   |   40%    |         |
|-----------------------------------------|
|  1.7     | 0.002   |   43%    |         |
\-----------------------------------------/
Run Code Online (Sandbox Code Playgroud)

预期产出

  A test table with merged cells
/----------------------------- \
|      Average      |   Red    |          
|-------------------|  eyes    |          
|  height |  weight |          |          
|------------------------------|
|  1.9     | 0.003   |   40%   |          
|------------------------------|
|  1.7     | 0.002   |   43%   |          
\------------------------------/
Run Code Online (Sandbox Code Playgroud)

Nau*_*hal 12

删除代码中的额外TH

http://jsfiddle.net/yqQsP/

<html>
<body>

<TABLE border="1" >

<CAPTION><EM>A test table with merged cells</EM></CAPTION>

<TR>
    <TH colspan="2"> Average</TH>
    <TH rowspan="2">Red<BR>eyes </TH>
</TR>
<TR>
  <TH>height</TH><TH>weight</TH>
</TR>
<TR> 
  <TD>1.9<TD>0.003<TD>40%</TD>
</TR>
<TR> 
  <TD>1.7<TD>0.002<TD>43%</TD>
</TR>

</TABLE>
</body>
</html>
Run Code Online (Sandbox Code Playgroud)


Dav*_*mas 7

虽然nauphal已经解决了你的问题,但我只是想对你的HTML结构提出一些建议.

首先,大写不是强制性的(HTML不区分大小写),但是你应该切换到XHTML小写必须的(坦率地说,看起来也好一点).

其次,因为tbody浏览器总是插入一个元素(我不确定所有客户端,但肯定是可视化Web客户端)如果已经没有一个存在,通常最好包装那些代表'body'的元素在tbody你自己的表中,你可以将th元素行分配给a thead,这会稍微增加语义(我不确定它是多么有用,但每一点都有帮助).

第三,记得关闭你的标签:

<TR> 
  <TD>1.9<TD>0.003<TD>40%</TD>
</TR>
Run Code Online (Sandbox Code Playgroud)

真的应该是:

<TR> 
  <TD>1.9</TD><TD>0.003</TD><TD>40%</TD>
</TR>
Run Code Online (Sandbox Code Playgroud)

同样,它不是强制性的(在HTML 4中,我相信),但它通过在标记周围添加额外的,未关闭的开始标记来减少引入错误的范围.

第四,这只是挑剔,可能,如果你想要整个caption强调文本的样式,更容易避免插入额外的标记,只是caption直接样式.

也就是说,这是我的表格版本和一些CSS:

<table>
    <caption>A test table with merged cells</caption>
    <theader>
        <tr>
            <th colspan="2">Average</th>
            <th rowspan="2">Red Eyes</th>
        </tr>
        <tr>
            <th>height</th>
            <th>weight</th>
        </tr>
    </theader>
    <tbody>
        <tr>
            <td>1.9</td>
            <td>0.003</td>
            <td>40%</td>
        </tr>
        <tr>
            <td>1.7</td>
            <td>0.002</td>
            <td>43%</td>
        </tr>
    </tbody>
</table>?
Run Code Online (Sandbox Code Playgroud)

CSS:

caption {
    font-style: italic;
}

td,
th {
    border: 1px solid #000;
    padding: 0.2em;
}?
Run Code Online (Sandbox Code Playgroud)

JS小提琴.