html - 有没有办法让一个 <td> 显示在它自己的行上?

标签 html css

我需要让一行包含 4 个单元格,如下所示:前三个单元格在一行上,最后一个单元格在前三个单元格下面占一行,跨越所有这些单元格。像这样:

----------------------------------  <---Start Row
|          |          |          |
|  Cell 1  |  Cell 2  |  Cell 3  | 
|          |          |          |
----------------------------------
|                                |
|              Cell 4            |
|                                |
----------------------------------  <---End Row

在 HTML 中有没有办法做到这一点?

最佳答案

您正在寻找 colspan属性...

<tr>
  <td>Cell 1</td>
  <td>Cell 2</td>
  <td>Cell 3</td>
</tr>
<tr>
  <td colspan="3">Cell 4</td>
</tr>

colspan 允许您将单个单元格“跨越”到多个。此属性的垂直伙伴是 rowspan,它允许您将单个单元格跨越多个


但是,在直接回答您的问题时,我认为一行中的第 4 个单元格不可能出现在下一行。

我的理解是,您必须创建 2 个单独的行才能实现您的目标。 (如果有人证明我错了,我很乐意删除我的答案)


基于以上内容,如果 jQuery 答案是可以接受的,您可以执行以下操作...

$(function() {
  // Get the last child, detach from the row and add colspan
  $lastTd = $("table tr td:last-child").detach().attr("colspan", "3");
  // Create a new row, add the detached cell, and add to the table
  $("table").append($("<tr></tr>").append($lastTd));
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<table border="1">
  <tr>
    <td>Cell 1</td>
    <td>Cell 2</td>
    <td>Cell 3</td>
    <td>Cell 4</td>
  </tr>
</table>

关于html - 有没有办法让一个 <td> 显示在它自己的行上?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35850487/

相关文章:

android - 为什么我的网站的移动版上有笑脸和空白?

javascript - 如何使用 jquery 和 div 标签将文本添加到现有的 div 想要根据文本自动增加高度

html - 如何在绝对定位的文本上实现与分词相同的效果?

javascript - jquery如何定位:fixed an element until the end of a certain div of page

html - Bootstrap 3 : Label on top on field and submit button in a form inline

html - Chrome 中的 CSS3 过渡故障

javascript - 动态元素上的 CSS 破坏了布局

html - 全宽响应式下拉菜单

html - 在两个 div 中拆分页面,当滚动时背景颜色不会填充两个 div 上的 100% 高度

angularjs 弹出窗口不适用于路由模板和 Controller