I am interested in extracting all the content from div.class1
except for [...] div.class4
To achieve this, you can utilize the attribute selector:
div[class]
targets any div
with a class
attribute.
div[class="class4"]
selects any div
with a class
attribute specifically set to class4
.
div[class!="class4"]
picks any div
with a class
attribute that is not equal to class4
.
Therefore, your required selector would be:
div div[class!="class4"]
This targets any div
inside another div
where the class name is not class4
.
Here is a Functional Example:
$(document).ready (
function() {
$('div div[class!="class4"]').css('borderWidth','3px');
}
);
div {
margin: 12px;
width: 50%;
border: 1px solid rgb(0,0,0);
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="class1">
<div class="class2">
<div class="class3">
some text
</div>
<div class="class4">
some text
</div>
</div>
</div>
For thoroughness, here are the jQuery and JavaScript equivalents...
Using jQuery:
function() {
$('div div[class!="class4"]').css('borderWidth','3px');
}
Using JavaScript:
var selectedDivs = document.querySelectorAll('div div:not(.class4)');
for (var i = 0; i < selectedDivs.length; i++) {
selectedDivs[i].style.borderWidth = '3px';
}