如果在课堂上有效的话......
$('.priority').children('.no-priority').addClass('normal');
如果缺少子类.no-priority,我将如何添加一个类?
尝试下面的代码不起作用
if (!$('.priority').children('.no-priority')) {
$('.priority').addClass('prioritised');
}
也没有
$('.priority').not('.no-priority').addClass('prioritised');
答案 0 :(得分:2)
您可以使用:not()
,:has()
$(".priority:not(:has(.no-priority))").addClass("normal")

.normal {
color: green;
}

<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="priority">
<div class="does-not-have-no-priority">
does not have .no-priority
</div>
</div>
<div class="priority">
<div class="no-priority">
has .no-priority
</div>
</div>
&#13;
答案 1 :(得分:0)
使用
filter
$('.priority').filter(function() {
return $(this).find('.no-priority').length === 0;
}).addClass('normal');
&#13;
答案 2 :(得分:0)
如果我理解你的问题,你可以在vanilla javascript中执行此操作:
var priority = document.querySelectorAll('.priority');
for (var i = 0; i < priority.length; i++) {
if (priority[i].querySelector('.no-priority')) {
priority[i].classList.add('normal');
} else {
priority[i].classList.add('prioritised');
}
}
.priority.normal {
color: red;
}
.priority.prioritised {
color: blue;
}
<div class="priority">
<div class="no-priority">no priority</div>
</div>
<div class="priority">
<div class="foo">priority</div>
</div>