大网数据广告活动
Web 标准与前端开发

HTMLTableCellElement:colSpan 属性

· 52CE 我爱测

HTMLTableCellElement 接口的 **`colSpan`** 只读属性表示此单元格必须跨越的列数;这允许单元格在表格的多列之间占据空间。它反映 `colspan` 属性。

HTML DOM

HTMLTableCellElement 接口的 colSpan 只读属性表示此单元格必须跨越的列数;这允许单元格在表格的多列之间占据空间。它反映 colspan 属性。

值

一个表示列数的正数。

[!NOTE] 当设置新值时,该值会被钳制到最接近的严格正数。

示例

示例提供了两个按钮来修改主体第一个单元格的列跨度。

HTML

<table>
  <thead>
    <tr>
      <th>列 1</th>
      <th>列 2</th>
      <th>列 3</th>
      <th>列 4</th>
      <th>列 5</th>
      <th>列 6</th>
      <th>列 7</th>
      <th>列 8</th>
    </tr>
  </thead>
  <tbody>
    <tr>
      <td colspan="2">1</td>
      <td>2</td>
      <td>3</td>
      <td>4</td>
      <td>5</td>
      <td>6</td>
      <td>7</td>
      <td>8</td>
    </tr>
  </tbody>
</table>
<button id="increase">增加列跨度</button>
<button id="decrease">减少列跨度</button>
<div>第一个单元格跨越 <output>2</output> 列。</div>
table {
  border-collapse: collapse;
}

th,
td,
table {
  border: 1px solid black;
}

button {
  margin: 1em 1em 1em 0;
}

JavaScript

// 获取相关接口元素
const cell = document.querySelectorAll("tbody tr td")[0];
const output = document.querySelectorAll("output")[0];

const increaseButton = document.getElementById("increase");
const decreaseButton = document.getElementById("decrease");

increaseButton.addEventListener("click", () => {
  cell.colSpan = cell.colSpan + 1;

  // 更新显示
  output.textContent = cell.colSpan;
});

decreaseButton.addEventListener("click", () => {
  cell.colSpan = cell.colSpan - 1;

  // 更新显示
  output.textContent = cell.colSpan;
});

结果

示例

规范

浏览器兼容性

参见

  • HTMLTableCellElement.rowSpan
  • HTMLTableColElement.span

来源:MDN Web Docs / Mozilla 贡献者 · 作者与贡献记录 · CC BY-SA 2.5。离线整理了版式、链接和媒体显示。

资料来源与适用范围

资料来源
MDN Web Docs / Mozilla 贡献者
适用范围
请按文中版本与环境说明核对
内容语言
中文;命令、路径和错误标识保留技术原文
本地收录日期
2026-11-15
授权说明
CC BY-SA 2.5

正文与所需资源由本站本地存储。查看来源及更新记录 ↗

请按适用版本和实际日志确认原因;本站没有对每一种系统或设备逐项执行文中的操作。