Вы можете сделать это легко, просто проверив, одинакова ли длина всех дочерних элементов с классом .hide
или нет. Если да, то добавьте класс к родительскому, иначе удалите его.
$(".compare-group-row").each(function() {
if($('.attribute-rows .matching-attribute.hide', this).length === $('.attribute-rows .matching-attribute', this).length)
$(this).addClass('hide');
else
$(this).removeClass('hide');
});
Демо:
$(".compare-group-row").each(function() {
if($('.attribute-rows .matching-attribute.hide', this).length === $('.attribute-rows .matching-attribute', this).length)
$(this).addClass('hide');
else
$(this).removeClass('hide');
});
.compare-group-row {
padding: 5px; border: 1px solid red;
margin: 5px 8px;
}
.compare-group-row.hide {
background-color:#CCC;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div class="compare-group-row active">
<div class="flex-table group-row row" role="rowgroup">
<div class="flex-row frist" role="cell">
<span class="attribute label">ITEM 1</span>
</div>
<div class="flex-row" role="cell"></div>
<div class="flex-row" role="cell"></div>
</div>
<div class="attribute-rows">
<div class="flex-table attribute-row row matching-attribute hide" role="rowgroup">
<div class="flex-row first" role="cell">ITEM 1</div>
<div class="flex-row">VALUE 1</div>
<div class="flex-row">VALUE 1</div>
</div>
<div class="flex-table attribute-row row matching-attribute hide" role="rowgroup">
<div class="flex-row first" role="cell">ITEM 1</div>
<div class="flex-row">VALUE 1</div>
<div class="flex-row">VALUE 1</div>
</div>
<div class="flex-table attribute-row row matching-attribute hide" role="rowgroup">
<div class="flex-row first" role="cell">ITEM 1</div>
<div class="flex-row">VALUE 1</div>
<div class="flex-row">VALUE 1</div>
</div>
<div class="flex-table attribute-row row matching-attribute hide" role="rowgroup">
<div class="flex-row first" role="cell">ITEM 1</div>
<div class="flex-row">VALUE 1</div>
<div class="flex-row">VALUE 1</div>
</div>
</div>
</div>
<div class="compare-group-row active">
<div class="flex-table group-row row" role="rowgroup">
<div class="flex-row frist" role="cell">
<span class="attribute label">ITEM 2</span>
</div>
<div class="flex-row" role="cell"></div>
<div class="flex-row" role="cell"></div>
</div>
<div class="attribute-rows">
<div class="flex-table attribute-row row matching-attribute hide" role="rowgroup">
<div class="flex-row first" role="cell">ITEM 1</div>
<div class="flex-row">VALUE 1</div>
<div class="flex-row">VALUE 1</div>
</div>
<div class="flex-table attribute-row row matching-attribute" role="rowgroup">
<div class="flex-row first" role="cell">ITEM 1</div>
<div class="flex-row">VALUE 1</div>
<div class="flex-row">VALUE 2</div>
</div>
<div class="flex-table attribute-row row matching-attribute" role="rowgroup">
<div class="flex-row first" role="cell">ITEM 1</div>
<div class="flex-row">VALUE 1</div>
<div class="flex-row">VALUE 2</div>
</div>
<div class="flex-table attribute-row row matching-attribute hide" role="rowgroup">
<div class="flex-row first" role="cell">ITEM 1</div>
<div class="flex-row">VALUE 1</div>
<div class="flex-row">VALUE 1</div>
</div>
</div>
</div>
Или вы также можете сделать это в нескольких строках, используя метод .toggleClass()
, например:
$(".compare-group-row").each(function() {
$(this).toggleClass('hide', $('.attribute-rows .matching-attribute.hide', this).length === $('.attribute-rows .matching-attribute', this).length);
});
Демо:
$(".compare-group-row").each(function() {
$(this).toggleClass('hide', $('.attribute-rows .matching-attribute.hide', this).length === $('.attribute-rows .matching-attribute', this).length);
});
.compare-group-row {
padding: 5px; border: 1px solid red;
margin: 5px 8px;
}
.compare-group-row.hide {
background-color:#CCC;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div class="compare-group-row active">
<div class="flex-table group-row row" role="rowgroup">
<div class="flex-row frist" role="cell">
<span class="attribute label">ITEM 1</span>
</div>
<div class="flex-row" role="cell"></div>
<div class="flex-row" role="cell"></div>
</div>
<div class="attribute-rows">
<div class="flex-table attribute-row row matching-attribute hide" role="rowgroup">
<div class="flex-row first" role="cell">ITEM 1</div>
<div class="flex-row">VALUE 1</div>
<div class="flex-row">VALUE 1</div>
</div>
<div class="flex-table attribute-row row matching-attribute hide" role="rowgroup">
<div class="flex-row first" role="cell">ITEM 1</div>
<div class="flex-row">VALUE 1</div>
<div class="flex-row">VALUE 1</div>
</div>
<div class="flex-table attribute-row row matching-attribute hide" role="rowgroup">
<div class="flex-row first" role="cell">ITEM 1</div>
<div class="flex-row">VALUE 1</div>
<div class="flex-row">VALUE 1</div>
</div>
<div class="flex-table attribute-row row matching-attribute hide" role="rowgroup">
<div class="flex-row first" role="cell">ITEM 1</div>
<div class="flex-row">VALUE 1</div>
<div class="flex-row">VALUE 1</div>
</div>
</div>
</div>
<div class="compare-group-row active">
<div class="flex-table group-row row" role="rowgroup">
<div class="flex-row frist" role="cell">
<span class="attribute label">ITEM 2</span>
</div>
<div class="flex-row" role="cell"></div>
<div class="flex-row" role="cell"></div>
</div>
<div class="attribute-rows">
<div class="flex-table attribute-row row matching-attribute hide" role="rowgroup">
<div class="flex-row first" role="cell">ITEM 1</div>
<div class="flex-row">VALUE 1</div>
<div class="flex-row">VALUE 1</div>
</div>
<div class="flex-table attribute-row row matching-attribute" role="rowgroup">
<div class="flex-row first" role="cell">ITEM 1</div>
<div class="flex-row">VALUE 1</div>
<div class="flex-row">VALUE 2</div>
</div>
<div class="flex-table attribute-row row matching-attribute" role="rowgroup">
<div class="flex-row first" role="cell">ITEM 1</div>
<div class="flex-row">VALUE 1</div>
<div class="flex-row">VALUE 2</div>
</div>
<div class="flex-table attribute-row row matching-attribute hide" role="rowgroup">
<div class="flex-row first" role="cell">ITEM 1</div>
<div class="flex-row">VALUE 1</div>
<div class="flex-row">VALUE 1</div>
</div>
</div>
</div>