單行程式碼
// 刪除 #sNews 表格中索引為 JQID 的 tr(索引從 0 開始)
$('#sNews table tr').eq(JQID).remove();
完整範例
<div id="sNews">
<table>
<tr><td>第 1 列</td></tr>
<tr><td>第 2 列</td></tr>
<tr><td>第 3 列</td></tr>
</table>
</div>
<button onclick="deleteRow(1)">刪除第 2 列</button>
<script>
function deleteRow(index) {
$('#sNews table tr').eq(index).remove();
}
</script>
進階:帶動畫刪除
function deleteRowAnim(index) {
var $tr = $('#sNews table tr').eq(index);
$tr.fadeOut(300, function(){ $(this).remove(); });
}
注意
.eq() 索引從 0 開始,負數從後往前(-1 為最後一列)。
- 若表格有
<thead>/<tbody>,選擇器需精確:$('#sNews tbody tr')。
- 刪除後若需重新排序序號,可遍歷剩餘列更新第一欄文字。