HTMLTableColElement: span 屬性

Baseline 已廣泛支援

此特性已相當成熟,可在許多裝置和瀏覽器版本上使用。自 ⁨2015 年 7 月⁩以來,各瀏覽器均已提供此特性。

HTMLTableColElement 介面的 span 屬性表示此 <col><colgroup> 必須跨越的列數;這允許該列佔用表格中多個列的空間。它反映了 span 屬性。

一個表示列數的正數。

注意: 設定新值時,該值會被限制為最接近的嚴格正數(最多 1000)。

示例

此示例提供了兩個按鈕來修改表格主體第一單元格的列跨度。

HTML

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">Row 1</th>
      <td>cell</td>
      <td>cell</td>
      <td>cell</td>
      <td>cell</td>
    </tr>
  </tbody>
</table>
<button id="increase">Increase column span</button>
<button id="decrease">Decrease column span</button>
<div>The first &lt;col&gt; spans <output>2</output> actual column(s).</div>

CSS

css
.multiColumn {
  background-color: #d7d9f2;
}

JavaScript

js
// Obtain relevant interface elements
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 += 1;

  // Update the display
  output.textContent = col.span;
});

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

  // Update the display
  output.textContent = col.span;
});

結果

規範

規範
HTML
# dom-colgroup-span

瀏覽器相容性

另見