2012-06-14 48 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