我有这段代码:
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="view-poke">
<div class="content">
<div class="field"><a href="" class="active"></a></div>
</div>
</div>
&#13;
我想在.view-poke
有课程<a></a>
时将课程添加到.active
答案 0 :(得分:2)
循环浏览每个a.active
课程,并将自定义class
添加到其parent
$(function() {
$('a.active').each(function() {
$(this).closest('.view-poke').addClass('customClass')
})
})
&#13;
.customClass {
background-color: orange;
}
&#13;
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="view-poke">
<div class="content">
<div class="field"><a href="" class="active">Anchor 1</a>
</div>
</div>
</div>
<div class="view-poke">
<div class="content">
<div class="field"><a href="">Anchor 2</a>
</div>
</div>
</div>
<div class="view-poke">
<div class="content">
<div class="field"><a href="" class="active">Anchor 3</a>
</div>
</div>
</div>
<div class="view-poke">
<div class="content">
<div class="field">
<a href=""></a>Anchor 4</div>
</div>
</div>
&#13;
答案 1 :(得分:0)
这可能是你在寻找
$('a.active').each(function() {
$(this).parent().parent().addClass('.view-poke');
});