テーブルinsertRow()メソッド
例
テーブルの最初の位置に新しい行を挿入します(そして、コンテンツを含む<td>要素を挿入します)。
// Find a <table> element with id="myTable":
var table = document.getElementById("myTable");
// Create an empty <tr> element and add it to the 1st position of the table:
var row = table.insertRow(0);
// Insert new cells (<td> elements) at the 1st and 2nd position of the "new" <tr> element:
var cell1 = row.insertCell(0);
var cell2 = row.insertCell(1);
// Add some text to the new cells:
cell1.innerHTML = "NEW CELL1";
cell2.innerHTML = "NEW CELL2";
定義と使用法
insertRow()メソッドは、空の<tr>要素を作成し、それをテーブルに追加します。
insertRow()メソッドは、テーブルの指定されたインデックスに新しい行を挿入します。
注: <tr>要素には、1つ以上の<th>または<td>要素が含まれている必要があります。
ヒント:行を削除するには、 deleteRow()メソッドを使用します。
ブラウザのサポート
Method | |||||
---|---|---|---|---|---|
insertRow() | Yes | Yes | Yes | Yes | Yes |
構文
tableObject.insertRow(index)
パラメータ値
Value | Description |
---|---|
index | Required in Firefox and Opera, optional in IE, Chrome and Safari. A number that specifies the position of the row to insert (starts at 0). The value of 0 results in that the new row will be inserted at the first position. The value of -1 can also be used, this results in a new row being inserted at the last position. This parameter is required in Firefox and Opera, but optional in Internet Explorer, Chrome and Safari.If this parameter is omitted, insertRow() inserts a new row at the last position in Chrome, IE, Firefox and Opera, and at the first position in Safari. |
技術的な詳細
戻り値: | 挿入された<tr>要素 |
---|
その他の例
例
行の作成と削除:
function myCreateFunction() {
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";
}
function myDeleteFunction() {
document.getElementById("myTable").deleteRow(0);
}
関連ページ
HTMLリファレンス:HTML <tr>タグ
❮テーブルオブジェクト