2013-07-26 43 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 

}); 

希望它可以幫助

相關問題