我不知道如何做到这一点
我有
<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
悬停在其上时,它将仅选择 a 的 .child
,而当将 .b
悬停在其上时,它将选择 b 的仅.child
这应该涉及 this
或 $this
或 $(this)
或类似的东西..但它令人困惑,我不知道哪里可以阅读相关内容
最佳答案
这将选择具有 .child
类的所有子级。
$(".parent").mousemove(function() {
var children = $(this).children('.child');
});
这样,您就可以使用 .eq()
方法选择第一个子项。
if (children.length > 0) {
var firstChild = children.eq(0);
}
您还可以使用函数 .find()
从后代(从 child 的 child ...)中进行选择,这不是问题的一部分,但相关且有用。
var descendants = $(this).find('.child');
关于jquery - 在事件处理程序中选择具有类的子 div,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15597689/