2013-03-24 37 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