← 返回首页
HTMLTableCellElement: colSpan-Eigenschaft - Web-APIs | MDN

Dieser Inhalt wurde automatisch aus dem Englischen übersetzt, und kann Fehler enthalten. Erfahre mehr über dieses Experiment.

View in English Always switch to English

HTMLTableCellElement: colSpan-Eigenschaft

Baseline Weitgehend verfügbar

Diese Funktion ist gut etabliert und funktioniert auf vielen Geräten und in vielen Browserversionen. Sie ist seit Juli 2015 browserübergreifend verfügbar.

Die colSpan-Eigenschaft des HTMLTableCellElement-Interfaces repräsentiert die Anzahl der Spalten, die diese Zelle umspannen muss. Dadurch kann die Zelle Raum über mehrere Spalten der Tabelle einnehmen. Sie entspricht dem colspan-Attribut.

In diesem Artikel

Wert

Eine positive Zahl, die die Anzahl der Spalten darstellt.

Hinweis: Beim Setzen eines neuen Wertes wird der Wert auf die nächste strikt positive Zahl geklammert.

Beispiele

Dieses Beispiel bietet zwei Schaltflächen, um die Spaltenanzahl der ersten Zelle des Körpers zu ändern.

HTML

html
<table> <thead> <tr> <th>Col 1</th> <th>Col 2</th> <th>Col 3</th> <th>Col 4</th> <th>Col 5</th> <th>Col 6</th> <th>Col 7</th> <th>Col 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">Increase colspan</button> <button id="decrease">Decrease colspan</button> <div>The first cell spans <output>2</output> column(s).</div>
table { border-collapse: collapse; } th, td, table { border: 1px solid black; } button { margin: 1em 1em 1em 0; }

JavaScript

js
// Obtain relevant interface elements 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 += 1; // Update the display output.textContent = cell.colSpan; }); decreaseButton.addEventListener("click", () => { cell.colSpan -= 1; // Update the display output.textContent = cell.colSpan; });

Ergebnis

Spezifikationen

Spezifikation
HTML
# dom-tdth-colspan

Browser-Kompatibilität

JavaScript aktivieren, um diese Browser-Kompatibilitätstabelle anzuzeigen.

Siehe auch