2012-06-14 46 views
0

比方說,我有一個這樣的名單:選擇的DIV

<ul class="list"> 
<li><span class="pos"><div class="txt_pos">1</div></li> 
<li><span class="pos"><div class="txt_pos">2</div></li> 
<li><span class="pos"><div class="txt_pos">3</div></li> 
<li><span class="pos"><div class="txt_pos">4</div></li> 
<li><span class="pos"><div class="txt_pos">5</div></li> 
</ul> 

和我的JS:

$(".list span.pos").each(function(i) { 
    var newOne = i; 
    newRank = getNth(newOne); 

    $("> .txt_pos").slideToggle('slow'); 
    $(this).text(newRank); 

    $("> .txt_pos").slideToggle('slow');       
}); 

我如何讓它選擇每個li因爲現在,它做每個列表項目在一次。我試圖選擇.pos的孩子。

+1

你試過'$( 「txt_pos」,這一點)的''而不是$( 「> .txt_pos」)'? – Engineer

+2

很少的提示 - 'span'沒有結束標記。不要在''inline''元素中放置'div'等'block'元素,例如'span' – Bongs

+0

@Engineer。我不是,我的回答是更快。 – gdoron

回答

1
$(".list span.pos").each(function(i) { 
     var newOne = i; 
     newRank = getNth(newOne); 

     $(this).children('.txt_pos').slideToggle('slow'); 
     $(this).text(newRank); 

     $(this).children('.txt_pos').slideToggle('slow'); //not sure why you're doing this again? 
)}; 
+0

您可能想緩存'$(this).children('。txt_pos')' – vzwick

3

使用.children()選擇一個孩子。

$(this).children('.txt_pos') 

或者,如果你想選擇的li(你似乎在說兩者),使用.parent()

$(this).parent() 
+0

+1。還有其他問題,''......? – gdoron

+0

@gdoron:對。這裏有一些不好的HTML,包括大概在'span'內的'div'。 – 2012-06-14 12:13:08