2017-06-19 70 views
1

的孩子,我怎么可以针对第二元素的子用jQuery:我怎样才能针对第二个元素的jQuery

<div class="item active"></div> 
<div class="item active"> 
    <div class="overlay"></div> 
</div> 
<div class="item active"></div> 
<div class="item active"></div> 

我尝试这些:

$(".item .active:nth-child(2) .overlay").addClass("test"); 
$(".item .active:nth-child(2)").find("overlay").addClass("test"); 

但没有奏效。

我做错了什么,我该如何瞄准它?

+1

您的格式是错误的。删除项目和活动之间的空间。 $(“。item.active:nth-child(2).overlay”)。addClass(“test”); $(“。item.active:nth-child(2)").find("overlay").addClass("test”); – Korgrue

回答

1

只是.item。主动

$(".item.active:nth-child(2) .overlay").addClass("test"); 
$(".item.active:nth-child(2)").find("overlay").addClass("test"); 

之间移除空间,然后他们都做工精细

看到我的代码:https://codepen.io/miladfm/pen/jwBpZo

相关问题