Metoda TableRow insertCell()
Przykład
Wstaw nowe komórki z zawartością na początku wiersza tabeli z id="myRow":
var row = document.getElementById("myRow");
var x = row.insertCell(0);
x.innerHTML = "New cell";
Definicja i użycie
Metoda insertCell() wstawia komórkę do bieżącego wiersza.
Porada: Użyj metody deleteCell() , aby usunąć komórkę w bieżącym wierszu tabeli.
Obsługa przeglądarki
Method | |||||
---|---|---|---|---|---|
insertCell() | Yes | Yes | Yes | Yes | Yes |
Składnia
tablerowObject.insertCell(index)
Wartości parametrów
Value | Description |
---|---|
index | Required in Firefox and Opera, optional in IE, Chrome and Safari. A number (starts at 0) that specifies the position of the new cell in the current row. The value of 0 results in that the new cell will be inserted at the first position. The value of -1 can also be used; which results in that the new cell will be inserted at the last position. If this parameter is omitted, insertCell() inserts the new cell at the last position in IE and at the first position in Chrome and Safari. This parameter is required in Firefox and Opera, but optional in Internet Explorer, Chrome and Safari. |
Szczegóły techniczne
Wartość zwrotu: | Wstawiony element komórki |
---|
Więcej przykładów
Przykład
Wstaw nowe komórki z zawartością na końcu wiersza tabeli z id="myRow":
var row = document.getElementById("myRow");
var x = row.insertCell(-1);
x.innerHTML = "New cell";
Przykład
Wstaw nowe komórki z zawartością w pozycji indeksu 2 wiersza tabeli z id="myRow":
var row = document.getElementById("myRow");
var x = row.insertCell(2);
x.innerHTML = "New cell";
Przykład
Wstaw nowe komórki na początku pierwszego wiersza tabeli. Kolekcja wierszy tabeli (.rows[0]) zwraca kolekcję wszystkich elementów <tr> w tabeli o identyfikatorze „myTable”. Liczba [0] określa element do pobrania, w tym przykładzie pierwszy wiersz tabeli. Następnie używamy insertcell(), aby wstawić nowe komórki na pozycji indeksu -1:
var firstRow = document.getElementById("myTable").rows[0];
var x = firstRow.insertCell(-1);
x.innerHTML = "New cell";
Przykład
Usuń pierwszą komórkę (komórki) z wiersza tabeli z id="myRow":
var row = document.getElementById("myRow");
row.deleteCell(0);
Przykład
Wstaw nowe wiersze na początku tabeli. Metoda insertRow() wstawia nowy wiersz o określonym indeksie w tabeli, w tym przykładzie pierwszą pozycję (początek) tabeli z identyfikatorem id="myTable". Następnie używamy metody insertCell(), aby dodać komórki w nowym wierszu.
var table = document.getElementById("myTable");
var row = table.insertRow(0);
var cell1 = row.insertCell(0);
var cell2 = row.insertCell(1);
cell1.innerHTML = "NEW CELL1";
cell2.innerHTML = "NEW CELL2";
❮ Obiekt TableRow