1

I am try to set click event on table every row with this.

This is table:

<table border='1px'>
    <tr>
        <td class='td'>first row frist column</td>
        <td>first row second column</td>
    </tr>
    <tr>
        <td class='td'>second row frist column</td>
        <td>second row second column</td>
    </tr>
    <tr>
        <td class='td'>third row frist column</td>
        <td>third row second column</td>
    </tr>
</table>

And this is simple jQuery:-

$('table tbody tr  td:nth-child(1)').live('click',function(){
alert($(this).text());
})

With this I click any of first column then on its alert I want to unset table first row column click event.

How can I do this.

4

6 回答 6

2

您可以使用:not()伪选择器从单击绑定中排除第一行。

$('table tbody tr:not(:first) td:first-child')

这会将点击处理程序附加到所有第一列,但第一行中的第一列除外。是上面的 jsFiddle。

于 2013-07-26T07:21:54.573 回答
2

试试这个,演示http://jsfiddle.net/yyene/CGbXP/4/

col+number 给所有列一个类。然后用...

var i = 1;
$('td').each(function(){    
    $(this).addClass('col'+i);
    i = i+1;
});

$('table').find("td:not(.col4)").on('click',function(){
    alert($(this).text());
})
于 2013-07-26T07:25:38.363 回答
1

不再推荐使用 .live() 方法,因为更高版本的 jQuery 提供了没有缺点的更好的方法

.live在最新版本的 jquery 中已弃用。

尝试这个

$('table tbody tr:not(:first)').on('click',function(){
alert($(this).text());
});

提琴手

于 2013-07-26T07:23:57.783 回答
1

试试这个代码

    $('table tbody tr:not(:first-child)  td').click(function(){
alert($(this).text());
})

http://jsfiddle.net/pepean/JP9EM/

于 2013-07-26T07:26:28.217 回答
1

您可以立即排除选择器中的第一行,方法是指定它只应用于索引大于 0 的所有行:

$('table tbody tr:gt(0) td:nth-child(1)').live('click',function(){
    alert($(this).text());
});

然而,在最近的 jQuery 版本中,.live在 1.10 中已被弃用甚至删除,因此您应该.on改用:

$('table tbody tr:gt(0) td:nth-child(1)').on('click',function(){
    alert($(this).text());
});

最后,如果你应该有嵌套表,你的选择器将返回太多的命中。选择直接子代:

$('table > tbody > tr:gt(0) > td:nth-child(1)').on('click',function(){
    alert($(this).text());
});

演示:http: //jsfiddle.net/2FkDY/

于 2013-07-26T07:28:17.563 回答
0

我认为您不必在第一行上课,然后使用该onclick事件。

$('.td').on('click', function() { 
     //do your stuff here

});

希望能帮助到你

于 2013-07-26T07:40:48.737 回答