<table><tr><td>构建基础表格
<table border="1">
<tr>
<td>张三</td>
<td>男</td>
<td>未婚</td>
</tr>
<tr>
<td>李四</td>
<td>女</td>
<td>已婚</td>
</tr>
</table>
解释:<table>元素表示一个表格的声明,<tr>元素表示表格的一行,<td>元素表示一个单元格。默认情况下表格是没有边框的,所以,在<table>元素增加一个border属性,设置为1即可显示边框。
<th>为表格添加标题单元格
<table border="1" style="width:300px;">
<tr>
<th>姓名</th>
<th>性别</th>
<th>婚否</th>
</tr>
<tr>
<td>张三</td>
<td>男</td>
<td>未婚</td>
</tr>
<tr>
<td>李四</td>
<td>女</td>
<td>已婚</td>
</tr>
</table>
解释:<th>元素主要是添加标题行的单元格,实际作用就是将内部文字居中且加粗。这里使用了一个通用属性style,主要用于CSS样式设置,以后会涉及到。<th><td>均属于单元格,包含两个合并属性:colspan、rowspan等。
<table border="1" style="width:300px;">
<tr>
<th rowspan="4">基本情况</th>
<th>姓名</th>
<th>性别</th>
<th>婚否</th>
</tr>
<tr>
<td>张三</td>
<td>男</td>
<td>未婚</td>
</tr>
<tr>
<td>李四</td>
<td>女</td>
<td>已婚</td>
</tr>
<tr>
<td colspan="3">统计:共两人</td>
</tr>
</table>
<thead>添加表头
<thead>
<tr>
<th>姓名</th>
<th>性别</th>
<th>婚否</th>
</tr>
</thead>
解释:<thead>元素就是限制和规范了表格的表头部分。尤其是以后动态生成表头,它的位置是不固定的,使用此元素可以限定在开头位置
<tfoot>添加表脚
<tfoot>
<tr>
<td colspan="3">统计:共两人</td>
</tr>
</tfoot>
解释:<tfoot>元素为表格生成表脚,限制在表格的底部。
<tbody>添加表主体
<tbody>
<tr style="background:red;">
<td>张三</td>
<td>男</td>
<td>未婚</td>
</tr>
<tr>
<td>李四</td>
<td>女</td>
<td>已婚</td>
</tr>
</tbody>
解释:<tbody>元素主要是包含住非表头表脚的主体部分,有助于表格格式的清晰,更加有助于后续CSS和JavaScript的控制。
<caption>添加表格标题
<caption>这是一个表格</caption>
解释:<caption>元素给表格添加一个标题。
汇合
<table border="1" style="width:300px;">
<caption>这是一个表格</caption>
<tfoot>
<tr>
<td colspan="3">统计:共两人</td>
</tr>
</tfoot>
<tbody>
<tr style="background:red;">
<td>张三</td>
<td>男</td>
<td>未婚</td>
</tr>
<tr>
<td>李四</td>
<td>女</td>
<td>已婚</td>
</tr>
</tbody>
<thead>
<tr>
<th>姓名</th>
<th>性别</th>
<th>婚否</th>
</tr>
</thead>
</table>
<colgroup>设置列颜色
<colgroupspan="2"style="background:red;">
<colgroup style="background:white;" span="1"></colgroup>
<colgroup style="background:red;" span="1"></colgroup>
或者
<colgroup>
<col>
<col style="background:red;">
</colgroup>
解释:<colgroup>元素是为了处理某个列,span属性定义处理哪些列。1表示第一列,2表示前两列。如果要单独设置第二列,那么需要声明两个,先处理第一个,将列点移入第二位,再处理第二个即可。