jQuery的:行程并选择第一要素的孩子sibilings类
问题描述:
如果我有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
元素?
答
$('.test1').hover(function() {
$(this).siblings('.test1:first').children('.first').css('color', 'blue');
});
答
将一组测试类的元素之内得到第一个span元素,语法是这样的:
$('span:first', $('.test1'))
或为第一类的第一要素。 。 。
$('.first:first', $('.test1'))
对于悬停功能,你可以做这样的事情:
$('span').hover(function() {
var element = $('.first:first', $('.'+$(this).attr('class')));
});
如果以此为test2的悬停功能不起作用,或者如果有任何元素都在测试1跨越之前。兄弟姐妹(':first')将不符合正确的要素。 – daxnitro 2012-02-13 01:43:33
该问题指定它应该只在“悬停任何且仅有那些.test1元素时”才起作用。至于选择其他兄弟姐妹的孩子,从所希望的问题中也不清楚:“兄弟姐妹班的第一个元素的孩子”。 – 2012-02-13 01:47:03