2013-03-24 80 views
2

我不知道如何做到這一點選擇孩子的div一類在事件處理中

<div class="a parent"> 
<div class="child"> 
</div> 
</div> 
<div class="b parent"> 
<div class="child"> 
</div> 
</div> 

我想這樣的事情(在僞代碼)

$(".parent").mousemove(function(){ 

select the `.child` which is the child of this div 


}) 

所以當.a被徘徊在它上面時只會選擇一個.child,而當.b被徘徊時它會選擇b的.child只有

這應該包括this$this$(this)或類似的東西..但它的混亂,我不知道從哪裏讀到它

+2

究竟是什麼讓你困惑? jQuery函數在[文檔](http://api.jquery.com/jQuery/#jQuery1)以及[所有遍歷方法](http://api.jquery.com/category/traversing) /樹遍歷/)。花一些時間閱讀API文檔是值得的。也許[入門](http://docs.jquery.com/Tutorials:Getting_Started_with_jQuery)教程對你也有幫助。 – 2013-03-24 11:08:48

回答

6

這將選擇所有的孩子與.child類。

$(".parent").mousemove(function() { 
    var children = $(this).children('.child'); 
}); 

有了這個,您可以使用.eq()方法選擇第一個孩子。

if (children.length > 0) { 
    var firstChild = children.eq(0); 
} 
使用功能 .find()

您也可以從後代中選擇(從孩子的孩子......),這個問題的,但相關的和有用的部分不知道。

var descendants = $(this).find('.child'); 
+0

你必須如此快速 – btevfik 2013-03-24 11:09:51

+1

我不能幫助它:( – 2013-03-24 11:11:05

+3

你可以做'$(this).children('。child')。first()'來選擇第一個'.child'子/後裔。 – 2013-03-24 11:14:02