具有复杂结构的 HTML 表格

标签 html html-table

我在创建下图中的表格时遇到困难:

complex table (图片本身没有出现,请点击链接)

我在 stackoverflow 上搜索过,但 answer没有太多解释。以下是仅针对复杂行的代码,即以包含“星期二”的单元格开头的行

<table border="1">
  <tr>
    <td rowspan="6">Tuesday</td>
    <td rowspan="2">8:00 am</td>
    <td rowspan="2">11:00 am</td>
    <td rowspan="3">XPath</td>
  </tr>
  <tr>
    <td rowspan="2">11:00 am</td>
    <td rowspan="2">2:00 pm</td>
    <td rowspan="3">XSL Transformations</td>
  </tr>
  <tr>
    <td rowspan="2">2:00 am</td>
    <td rowspan="2">5:00 pm</td>
  </tr>
</table>

在上面的代码中,我使用了三个 TR,预计会生成三行,但实际上只生成了一行。上面代码的结果如下所示。我不明白为什么以“11:00 am”开头的行,即带圆圈的行不在新行上。

actual result (图片本身没有出现,请点击链接)

最佳答案

您创建了 6 行的结构,但您的标记只有 3 行,并且第 2 行包含来自第 1 行的跨单元格和第 2 行中定义的单元格。这就是浏览器试图水平堆叠它们的原因。

要获得所需的结果,您应该向表中添加 3 个额外的行并在它们之间分配单元格,以便每个单元格都从正确的行开始(第 4 行中的“XSL 转换”,其他行中的第 1、3 和 5 行)行):请参见下面的第一个示例。

顺便说一句,同样的视觉效果可以只用 4 行来实现:见下面的第二个例子。

<table border="1">
  <tr>
    <td rowspan="6">Tuesday</td>
    <td rowspan="2">8:00 am</td>
    <td rowspan="2">11:00 am</td>
    <td rowspan="3">XPath</td>
  </tr>
  <tr></tr><!-- needed to accomodate cells with rowspan="2" from above -->
  <tr>
    <td rowspan="2">11:00 am</td>
    <td rowspan="2">2:00 pm</td>
  </tr>
  <tr>
    <td rowspan="3">XSL Transformations</td>
  </tr>
  <tr>
    <td rowspan="2">2:00 am</td>
    <td rowspan="2">5:00 pm</td>
  </tr>
  <tr></tr><!-- needed to accomodate cells with rowspan="2" from above -->
</table>

<hr>

<table border="1">
  <tr><!-- row 1 -->
    <td rowspan="4">Tuesday</td><!-- spans rows 1-4 -->
    <td>8:00 am</td><!-- spans row 1 only -->
    <td>11:00 am</td><!-- spans row 1 only -->
    <td rowspan="2">XPath</td><!-- spans rows 1-2 -->
  </tr>
  <tr><!-- row 2 -->
    <td rowspan="2">11:00 am</td><!-- spans rows 2-3 -->
    <td rowspan="2">2:00 pm</td><!-- spans rows 2-3 -->
  </tr>
  <tr><!-- row 3 -->
    <td rowspan="2">XSL Transformations</td><!-- spans rows 3-4 -->
  </tr>
  <tr><!-- row 4 -->
    <td>2:00 am</td><!-- spans row 4 only -->
    <td>5:00 pm</td><!-- spans row 4 only -->
  </tr>
</table>

关于具有复杂结构的 HTML 表格,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58710429/

相关文章:

html - 具有动态数据的垂直表

带有边框半径和背景渐变的 HTML 进度条

html - 将此表的内容与 CSS 对齐的最佳做法?

php - 为循环中的每个表行赋予唯一类

javascript - 使用 Jquery 从表中获取单元格值?

javascript - 从逗号分隔的字符串javascript创建html表

html - 如何使用 HTML 和 CSS 绘制带有文本的选项卡

javascript - 从 HTML 和 Javascript 文件读取 Access 数据库记录

html - 为什么我不能在这个页面上设置第二行第一列的宽度?

css - 在这里我遇到了最后一列的麻烦。我想要最后一列 bg-color 和边框到垂直滚动条