2016-05-13 46 views
0

我需要jQuery的聲明中找到「.findme」在此代碼:jQuery - 如何找到下面的孩子裏面的元素?

$('.clicky').click(function() { 
    // the .find() method doesn't do it 
    $(this).find('.findme').css('color', 'red'); 
}); 

<div class='clicky'></div> 
<div class='brother'> 
    <div class='findme'>Here I am</div> 
</div> 

我已經試過變化對.find().next().children(),我不能完全做出來....

回答

2

this$(this).find('.findme')實際上指的是被點擊(股利與類clicky的元素。

.find()實際搜索你怎麼稱呼它的元素的後裔,因爲「findme「不在」clicky「div內,它找不到它。

你應該使用jQuery的.next()函數來獲取同級立即「clicky」分區(這是「brother‘)以下,然後搜索那裏爲’findme」分區。

$(this).next().find(".findme").css("color", "red"); 
1

你可能可以嘗試改變

$('.clicky').click(function() { 
    // the .find() method doesn't do it 
    $(this).siblings().children('.findme').css('color', 'red'); 
}); 

,我認爲你錯過了點擊我的內文的DIV

<div class='clicky'>click me!!!</div> 
<div class='brother'> 
    <div class='findme'>Here I am</div> 
</div>