JQUERY:在.each循环中查找类
我需要检查每个 .archive_blocks 是否包含 .archive_blocks_fa_active。 如果是,则将 css 应用到 .archive_blocks_sa。
<div class="archive_blocks">
<a href="#" class="archive_blocks_fa_active">
<img width="142" height="142" src="#">
</a>
<div class="archive_blocks_name">
<a href="#" class="archive_blocks_sa">Ilustrations</a>
</div>
</div>
查询:
$('.archive_blocks').each(function(){
if ( $("a.archive_blocks_fa_active").parents(this).length == 1 ) {
alert('OK');
} else {
alert('NO');
}
});
I need check every .archive_blocks for containing .archive_blocks_fa_active.
If is it true, apply css to .archive_blocks_sa.
<div class="archive_blocks">
<a href="#" class="archive_blocks_fa_active">
<img width="142" height="142" src="#">
</a>
<div class="archive_blocks_name">
<a href="#" class="archive_blocks_sa">Ilustrations</a>
</div>
</div>
JQUERY:
$('.archive_blocks').each(function(){
if ( $("a.archive_blocks_fa_active").parents(this).length == 1 ) {
alert('OK');
} else {
alert('NO');
}
});
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
您可以使用
has()
方法将匹配元素集减少为具有与选择器或 DOM 元素匹配的后代的元素。You can use
has()
method reduces the set of matched elements to those that have a descendant that matches the selector or DOM element.假设我已经正确理解了您的问题,您不能只这样做而不用担心
each
:这使用与任何
a.archive_blocks_fa_active
元素相匹配的后代选择器是.archive_blocks
元素的后代。然后它找到相关的.archive_blocks_sa
(还有其他方法可以做到这一点,如果您愿意,可以使用parent
和next
),然后添加班级。Assuming I've understood your question correctly, can't you just do this and not bother with the
each
:This uses a descendant selector which matches any
a.archive_blocks_fa_active
element that is a descendant of an.archive_blocks
element. It then finds the related.archive_blocks_sa
(there would be other ways of doing that, perhaps usingparent
andnext
if you prefer) and then adds the class.