Metoda TableRow insertCell().
Příklad
Vložte nové buňky s obsahem na začátek řádku tabulky s id="myRow":
var row = document.getElementById("myRow");
var x = row.insertCell(0);
x.innerHTML = "New cell";
Definice a použití
Metoda insertCell() vloží buňku do aktuálního řádku.
Tip: Pomocí metody deleteCell() odstraňte buňku v aktuálním řádku tabulky.
Podpora prohlížeče
Method | |||||
---|---|---|---|---|---|
insertCell() | Yes | Yes | Yes | Yes | Yes |
Syntax
tablerowObject.insertCell(index)
Hodnoty parametrů
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. |
Technické údaje
Návratová hodnota: | Vložený element buňky |
---|
Další příklady
Příklad
Vložte nové buňky s obsahem na konec řádku tabulky s id="myRow":
var row = document.getElementById("myRow");
var x = row.insertCell(-1);
x.innerHTML = "New cell";
Příklad
Vložte novou buňku(y) s obsahem na pozici indexu 2 řádku tabulky s id="myRow":
var row = document.getElementById("myRow");
var x = row.insertCell(2);
x.innerHTML = "New cell";
Příklad
Vložte novou buňku (buňky) na začátek prvního řádku tabulky. Kolekce řádků tabulky (.rows[0]) vrací kolekci všech prvků <tr> v tabulce s id "myTable". Číslo [0] určuje prvek, který se má načíst, v tomto příkladu první řádek tabulky. Poté použijeme insertcell() k vložení nových buněk na pozici indexu -1:
var firstRow = document.getElementById("myTable").rows[0];
var x = firstRow.insertCell(-1);
x.innerHTML = "New cell";
Příklad
Odstraňte první buňku (buňky) z řádku tabulky s id="myRow":
var row = document.getElementById("myRow");
row.deleteCell(0);
Příklad
Vložte nový řádek (řádky) na začátek tabulky. Metoda insertRow() vloží nový řádek na zadaný index do tabulky, v tomto příkladu na první pozici (začátek) tabulky s id="myTable". Poté pomocí metody insertCell() přidáme buňky do nového řádku.
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";
❮ Objekt TableRow