4

我想让我的表格行可点击。除了第一列之外,所有列都必须是可点击的。但是,当他们单击该行时,我想实现这一点。

这是我到目前为止的代码:

    $('.table tbody tr').click( function (e) {
        alert ($(this).find('td').eq(1).text());
    } );

无论我在行中的哪个位置单击,此代码都会始终执行。但是,我希望所有表格单元格都应该是可点击的,除了第一个。

有可能吗?

4

3 回答 3

5

你可以这样做:

$('.table tbody tr').on('click', function(e) {
    if ($(e.target).closest('td:first-child').length) {
        return;
    }

    // your code
});

这表示“如果单击的元素是 atd:first-child或有一个祖先是 a td:first-child,则什么也不做;否则继续。”

jsFiddle

于 2013-11-01T13:58:39.067 回答
1

使用 CSS Not 选择器并跳过第一个 tr 元素:

$('.table tbody tr:not(:first-child)').click( function (e) {
    alert ($(this).find('td').eq(1).text());
} );
于 2013-11-01T14:22:24.897 回答
1

尝试使用这样的委托事件,

$('.table tbody tr').delegate( 'td', 'click', function() {
   alert ($(this).text());
    // implement your logic...

});

于 2013-11-01T14:28:12.437 回答