HTMLTableRowElement: phương thức insertCell()

Baseline Widely available

This feature is well established and works across many devices and browser versions. It’s been available across browsers since July 2015.

Phương thức insertCell() của giao diện HTMLTableRowElement chèn một ô mới (<td>) vào một hàng của bảng (<tr>) và trả về tham chiếu đến ô đó.

Note: insertCell() chèn ô trực tiếp vào hàng. Ô không cần được thêm riêng bằng Node.appendChild() như trường hợp sử dụng Document.createElement() để tạo phần tử <td> mới.

Tuy nhiên, bạn không thể dùng insertCell() để tạo phần tử <th> mới.

Cú pháp

js
insertCell()
insertCell(index)

Tham số

index Optional

Chỉ số ô của ô mới. Nếu index-1 hoặc bằng số lượng ô, ô được thêm vào cuối hàng. Nếu index bị bỏ qua, mặc định là -1.

Giá trị trả về

Một HTMLTableCellElement tham chiếu đến ô mới.

Ngoại lệ

IndexSizeError DOMException

Được ném ra nếu index lớn hơn số lượng ô.

Ví dụ

Ví dụ này sử dụng HTMLTableRowElement.insertCell() để thêm một ô mới vào hàng.

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();
});

Kết quả

Thông số kỹ thuật

Thông số kỹ thuật
HTML
# dom-tr-insertcell-dev

Tương thích trình duyệt

Xem thêm