2012-02-13 28 views
1

如果我有jQuery的:行程並選擇第一要素的孩子sibilings類

<span class="test1"><span class="first">first</span></span> 
<span class="test1">lorem</span> 
<span class="test1">lorem</span> 
<span class="test1">lorem</span> 

<span class="test2"><span class="first">first</span></span> 
<span class="test2">lorem</span> 

我怎麼只能選擇.test1 .first,任何徘徊,只有那些.test1元素?

回答

0
$('.test1').hover(function() { 
    $(this).siblings('.test1:first').children('.first').css('color', 'blue'); 
}); 
+0

如果以此爲test2的懸停功能不起作用,或者如果有任何元素都在測試1跨越之前。兄弟姐妹(':first')將不符合正確的要素。 – daxnitro 2012-02-13 01:43:33

+0

該問題指定它應該只在「懸停任何且僅有那些.test1元素時」才起作用。至於選擇其他兄弟姐妹的孩子,從所希望的問題中也不清楚:「兄弟姐妹班的第一個元素的孩子」。 – 2012-02-13 01:47:03

0

將一組測試類的元素之內得到第一個span元素,語法是這樣的:

$('span:first', $('.test1')) 

或爲第一類的第一要素。 。 。

$('.first:first', $('.test1')) 

對於懸停功能,你可以做這樣的事情:

$('span').hover(function() { 
    var element = $('.first:first', $('.'+$(this).attr('class'))); 
}); 
相關問題