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

HTMLTableColElement:span 属性

· 52CE 我爱测

HTMLTableColElement 接口的 **`span`** 只读属性表示 col 或 colgroup 必须横跨的列数;这使得该列占据表中多列的空间。它反映 `span` 属性。

HTML DOM

HTMLTableColElement 接口的 span 只读属性表示 col 或 colgroup 必须横跨的列数;这使得该列占据表中多列的空间。它反映 span 属性。

值

一个表示列数的正数。

[!NOTE] 设置新值时,该值将被限制为最接近的严格正数(最多 1000)。

示例

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

HTML

<table>
  <colgroup>
    <col />
    <col span="2" class="multiColumn" />
  </colgroup>
  <thead>
    <th></th>
    <th scope="col">C1</th>
    <th scope="col">C2</th>
    <th scope="col">C3</th>
    <th scope="col">C4</th>
  </thead>
  <tbody>
    <tr>
      <th scope="row">行 1</th>
      <td>cell</td>
      <td>cell</td>
      <td>cell</td>
      <td>cell</td>
    </tr>
  </tbody>
</table>
<button id="increase">增加列跨度</button>
<button id="decrease">减少列跨度</button>
<div>第一个 &lt;col&gt; 横跨 <output>2</output> 个实际列。</div>
table {
  border-collapse: collapse;
}

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

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

CSS

.multiColumn {
  background-color: #d7d9f2;
}

JavaScript

// 获取相关接口元素
const col = document.querySelectorAll("col")[1];
const output = document.querySelectorAll("output")[0];

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

increaseButton.addEventListener("click", () => {
  col.span = col.span + 1;

  // 更新显示
  output.textContent = col.span;
});

decreaseButton.addEventListener("click", () => {
  col.span = col.span - 1;

  // 更新显示
  output.textContent = col.span;
});

结果

示例

规范

浏览器兼容性

参见

  • HTMLTableCellElement.colSpan

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

资料来源与适用范围

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

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

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