Nic*_*nto 2 jquery children parent-child jquery-selectors
我不知道如何做到这一点
我有
<div class="a parent">
<div class="child">
</div>
</div>
<div class="b parent">
<div class="child">
</div>
</div>
Run Code Online (Sandbox Code Playgroud)
我想要这样的东西(在伪代码中)
$(".parent").mousemove(function(){
select the `.child` which is the child of this div
})
Run Code Online (Sandbox Code Playgroud)
因此,当.a它悬停在它上面时,它将.child仅选择一个,并且当.b它悬停在它上面时将.child仅选择b
这应该涉及this或$this或$(this)类似的东西..但它令人困惑,我不知道在哪里阅读它
这将选择所有.child班级的孩子.
$(".parent").mousemove(function() {
var children = $(this).children('.child');
});
Run Code Online (Sandbox Code Playgroud)
有了这个,你可以用.eq()方法选择第一个孩子.
if (children.length > 0) {
var firstChild = children.eq(0);
}
Run Code Online (Sandbox Code Playgroud)
您还可以使用该函数从子代(来自儿童的孩子......)中进行选择.find(),而不是问题的一部分,但相关且有用.
var descendants = $(this).find('.child');
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
4088 次 |
| 最近记录: |