2009-08-01 81 views
0
<tr> 
<td>#</td> 
<td>2009</td> 
<td><a class="delete_this">Click</a></td> 
</tr> 

我想要使用jquery,并在点击锚点时获取第二个(第二个)“td”的文本。我想在同一个tr中的“td”作为锚...如何使用层次结构获取td的值?

我该怎么做?

到目前为止,我有

$(document).ready(function(){ 
    $(".delete_this').click(function(){ 
    var myNUmber = $(this).parent()....///And this i should write the code to get the text for second td in tr where the anchor belongs to 
}) 
}) 

回答

2

下面的几个方面:

$(this).parent().siblings("td:eq(1)").text() 

如果你想找的细胞然后才能这样做:

$(this).parent().prev().text() 
1

$('.delete_this').closest('tr').children(':eq(1)') .text();

1)获取.delete_this的标签
2)获取父TR
3)获得第二个TD
4)获取第二个TD的文本

+0

delete_this是'`。它没有兄弟姐妹 – ChssPly76 2009-08-01 00:29:27

1
var myNUmber = $(this).parent().siblings().get(1).text(); 

详细信息是here

1

您最好使用.live添加一个点击事件,而不是添加多个点击处理程序,如果您有大型表,这会影响性能(请考虑100个单独的绑定事件)。

还记得与节点名称的前缀类选择,如果你可以(在这里你确定所有delete_this是锚)

$('a.delete_this').live('click', function(){ 
    var myNUmber = $(this).parent().siblings().get(1).text(); 
});