2013-07-26 39 views
1

我尝试在表格每一行上设置点击事件。如何删除表格第一行上的点击事件

这是表:

<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> 

这是简单的jQuery: -

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

有了这个,我点击任何第一列然后在其警戒我想取消设置表的第一行栏点击事件。

我该怎么做。

回答

2

试试这个,DEMO http://jsfiddle.net/yeyene/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()); 
}) 
+0

我想在那里设置第二列ID ...怎么样? –

+0

检查更新回答n演示 – yeyene

+0

不,不......我想设置第一行第二列的id =“第二”像' 第一行弗里斯特列

2

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

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

这将click处理追加到所有的第一列,除了第一行中的第一列。 Here是上述的jsFiddle。

1

使用.live()方法,因为更高版本的jQuery报价 更好的方法没有缺点

.live被弃用最新版本的jQuery不再推荐。

试试这个

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

JsFiddle

+0

我想设置有第二列ID ...怎么样? –

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/

+0

我想要设置第一行第二列ID ...怎么办? –

0

我想你没有给一个类中的第一行上,然后用onclick事件。

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

}); 

希望它可以帮助

相关问题