2011-12-05 48 views
0

我有以下幾點:jQuery的:FAQ風格

<table class="site"> 
<tr> 
    <td> 
     <span style="float: right;" class='show'>Show More</span> 
    </td> 
</tr> 
<tr class="site_info"> 
    <td><%= site.id %></td> 
</tr> 
<tr> 
    <td align='right'> 
More 
    </td> 
</tr> 
</table> 

每當有人點擊 「.show」,我希望它顯示 「.site_info」(隱藏在啓動時)。我已經嘗試了很多東西,包括:

$('.site_info').hide().css('cursor', 'pointer'); 
$('.show').click(function() { 
    $(this).find('.site_info').slideToggle(); 
}); 

我似乎無法找到/選擇site_info部分。

回答

0
$('.site_info').hide().css('cursor', 'pointer'); 
$('.show').click(function() { 
    $(this).parents("table").find('.site_info').show("slideToggle"); 
}); 
0
$('.site_info').hide().css('cursor', 'pointer'); 
$('.show').click(function() { 
    $('.site_info').show("slow"); 
}); 
1

find命令只搜索當前元素的後代。

試試這個..

$(this).parents("table").find(".site-info").slideToggle(); 
+0

'find'搜索 「後裔」,不要與'children'混淆。 –

+0

啊,是的。你是對的。更新我的答案,謝謝。 – BZink

+0

如果有嵌套表(儘管希望沒有),還應該使用'nearest'而不是'parents'。 –

1
$(".site_info").hide().css("cursor", "pointer"); 
$(".show").click(function() { 
    $(this).closest("table.site").find("tr.site_info").slideToggle(); 
});