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

HTMLTableRowElement: Methode insertCell() - 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

HTMLTableRowElement: Methode insertCell()

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 insertCell()-Methode des HTMLTableRowElement-Interfaces erstellt ein <td>-Element, fgt es an der angegebenen Position in das gegebene <tr>-Element ein und gibt es zurck.

Diese Methode erstellt und fgt das Element direkt ein, ohne separate Aufrufe von Methoden wie Document.createElement(), Node.insertBefore() und Node.appendChild() zu erfordern. Sie knnen jedoch insertCell() nicht verwenden, um ein neues <th>-Element zu erstellen.

In diesem Artikel

Syntax

js
insertCell()
insertCell(index)

Parameter

index Optional

Der Index der neuen Zelle in der cells-Sammlung. Wenn index -1 oder gleich der Anzahl der Zellen ist, wird die Zelle als letzte Zelle in der Zeile angehngt. Wenn index weggelassen wird, ist der Standardwert -1.

Rckgabewert

Ein HTMLTableCellElement, das die neue Zelle referenziert.

Ausnahmen

IndexSizeError DOMException

Wird ausgelst, wenn index grer als die Anzahl der Zellen oder kleiner als -1 ist.

Beispiele

Dieses Beispiel verwendet HTMLTableRowElement.insertCell(), um eine neue Zelle an eine Zeile anzuhngen.

HTML

html
<table>
  <thead>
    <tr>
      <th>C1</th>
      <th>C2</th>
      <th>C3</th>
      <th>C4</th>
      <th>C5</th>
    </tr>
  </thead>
  <tbody>
    <tr>
      <td>Cell 1</td>
      <td>Cell 2</td>
    </tr>
  </tbody>
</table>

<button id="add">Add a cell</button>
<button id="remove">Remove last cell</button>
<div>This first row has <output>2</output> cell(s).</div>

JavaScript

js
// Obtain relevant interface elements
const bodySection = document.querySelectorAll("tbody")[0];
const row = bodySection.rows[0]; // Select the first row of the body section
const cells = row.cells; // The collection is live, therefore always up-to-date
const cellNumberDisplay = document.querySelectorAll("output")[0];

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

function updateCellNumber() {
  cellNumberDisplay.textContent = cells.length;
}

addButton.addEventListener("click", () => {
  // Add a new cell at the end of the first row
  const newCell = row.insertCell();
  newCell.textContent = `Cell ${cells.length}`;

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

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

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

Ergebnis

Spezifikationen

Spezifikation
HTML
# dom-tr-insertcell-dev

Browser-Kompatibilitt

Siehe auch


Web Proxy Viewer  |  New URL  |  Original Page