Phương thức xóa bảng ()
Thí dụ
Xóa hàng đầu tiên trong bảng:
document.getElementById("myTable").deleteRow(0);
Định nghĩa và Cách sử dụng
Phương thức deleteRow () xóa hàng tại chỉ mục được chỉ định khỏi bảng.
Mẹo: Sử dụng insertRow () để tạo và chèn một hàng mới.
Hỗ trợ trình duyệt
Method | |||||
---|---|---|---|---|---|
deleteRow() | Yes | Yes | Yes | Yes | Yes |
Cú pháp
tableObject.deleteRow(index)
Giá trị tham số
Value | Description |
---|---|
index | Required in Firefox and Opera, optional in IE, Chrome and Safari. An integer that specifies the position of the row to delete (starts at 0). The value of 0 results in that the first row will be deleted. The value of -1 can also be used; which result in that the last row will be deleted. This parameter is required in Firefox and Opera, but optional in Internet Explorer, Chrome and Safari. If this parameter is omitted, deleteRow() removes the last row in IE and the first row in Chrome and Safari. |
Chi tiết kỹ thuật
Giá trị trả lại: | Không có giá trị trả lại |
---|
Các ví dụ khác
Thí dụ
Xóa hàng bạn nhấp vào:
function deleteRow(r) {
var i = r.parentNode.parentNode.rowIndex;
document.getElementById("myTable").deleteRow(i);
}
Thí dụ
Tạo và xóa (các) hàng:
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);
}
Các trang liên quan
Tham chiếu HTML: Thẻ <tr> HTML
❮ Đối tượng bảng