DataTables 動態生成的表格,需針對第 3 列(索引 2)綁定點擊事件,點擊其他列無反應。
使用 nth-child 選擇器(注意:CSS 選擇器索引從 1 開始):
$('#example tbody').on('click', 'tr td:nth-child(3)', function(e) {
var name = $(this).text();
alert(name);
});
tr td:nth-child(3) 精確匹配每行的第 3 個 <td>。tbody,動態新增行自動生效。$(this) 直接指向被點擊的 td,無需 eq() 計算索引。若需獲取該行其他列數據,可用 $(this).closest('tr').find('td:eq(0)').text() 等方式。