[ Web Proxy ]
URL:
Viewing: https://developer.mozilla.org/de/docs/Web/API/HTMLTableSectionElement/rows [Back]  [Original]

HTMLTableSectionElement: rows-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

HTMLTableSectionElement: rows-Eigenschaft

Baseline Weitgehend verfgbar

Diese Funktion ist gut etabliert und funktioniert auf vielen Gerten und in vielen Browserversionen. Sie ist seit Juli 2015 browserbergreifend verfgbar.

Die rows-Eigenschaft der Schnittstelle HTMLTableSectionElement gibt eine schreibgeschtzte live HTMLCollection zurck, die die Zeilen im Abschnitt enthlt. Die HTMLCollection ist live und wird automatisch aktualisiert, wenn Zeilen hinzugefgt oder entfernt werden.

In diesem Artikel

Wert

Eine live HTMLCollection von HTMLTableRowElement-Objekten.

Beispiele

In diesem Beispiel ermglichen zwei Schaltflchen das Hinzufgen und Entfernen von Zeilen im Tabellenkrperabschnitt. Auerdem wird ein <output>-Element mit der aktuellen Anzahl der Zeilen in der Tabelle aktualisiert.

HTML

html
<table>
  <thead>
    <tr>
      <th>Col 1</th>
      <th>Col 2</th>
      <th>Col 3</th>
    </tr>
  </thead>
  <tbody>
    <tr>
      <td>X</td>
      <td>Y</td>
      <td>Z</td>
    </tr>
  </tbody>
</table>
<button id="add">Add a row</button>
<button id="remove">Remove last row</button>
<div>This table's body has <output>1</output> row(s).</div>

JavaScript

js
// Obtain relevant interface elements
const bodySection = document.querySelectorAll("tbody")[0];
const rows = bodySection.rows; // The collection is live, therefore always up-to-date
const rowNumberDisplay = document.querySelectorAll("output")[0];

const addButton = document.getElementById("add");
const removeButton = document.getElementById("remove");

function updateRowNumber() {
  rowNumberDisplay.textContent = rows.length;
}

addButton.addEventListener("click", () => {
  // Add a new row at the end of the body
  const newRow = bodySection.insertRow();

  // Add cells inside the new row
  ["A", "B", "C"].forEach(
    (elt) => (newRow.insertCell().textContent = `${elt}${rows.length}`),
  );

  // Update the row counter
  updateRowNumber();
});

removeButton.addEventListener("click", () => {
  // Delete the row from the body
  bodySection.deleteRow(-1);

  // Update the row counter
  updateRowNumber();
});

Ergebnis

Spezifikationen

Spezifikation
HTML
# dom-tbody-rows

Browser-Kompatibilitt

Siehe auch


Web Proxy Viewer  |  New URL  |  Original Page