2010-08-30 152 views
1

我在頁面上的給定<tr>的each()循環中。我該如何遍歷所有<th>$(this)即當前<tr>內:jquery循環問題

$('#my_table > tbody > tr').each(function() { 
    // how to loop through all the <th>'s inside the current tr? 
}); 

回答

1
$('#my_table > tbody > tr').each(function() { 
    $('th', this).each(...); 
}); 
-2
$('#my_table > tbody > tr').each(function() { 
    $(this).filter('th').each(function(){ 
    //your code here 
    }); 
}); 

但它可以更簡潔,如果你不需要在tr或執行任何操作完成可以鏈接它們。

+2

'filter' will remove來自_current_選擇器的任何不是「th」的元素,而不是得到「th」的孩子 – bdukes 2010-08-30 19:05:45

0
$(this).find('th').each(...) 

OR

$('th', this).each(...) 
1

你可以這樣做在原來的循環:

$('#my_table > tbody > tr > th').each(function() { 

,但如果你不能這樣做,出於某種原因,比如沒有在其他代碼循環,你可以使用這個:

$('th',this).each(function() {