Reputation: 112
$('#main div').not('#2 > #3').each(function() {
$(this).css('background-color', 'blue');
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div id="main">
<div id="1">
<div>Some text in paragraph 1</div>
<div>Some text in paragraph 1</div>
</div><br>
<div id="2">
<div id="3">Some text in paragraph 2</div>
<div>Some text in paragraph 1</div>
</div><br>
<div id="3">
<div>Some text in paragraph 3</div>
<div>Some text in paragraph 1</div>
</div><br>
</div>
I want the css not to be applied for the nested divs of div with id=2
. Any suggestions?
Upvotes: 3
Views: 54
Reputation: 12959
A) if your mean be #3
only:
default value for
background-color
istransparent
. you do not setbackground-color
for#3
for this reason it gettransparent
(in this case, you seeblue
color,because#2
havebackground-color:blue
).
your selector is correct just insert initial value for div
s:
div {
background-color:#fff;
}
B) If your mean be #2
and his childrens:
#2
to #a2
)Use this:
#main>div:not(#a2) {
background-color: blue!important;
}
#main>div:not(#a2) {
background-color: blue!important;
}
<div id="main">
<div id="1">
<div>Some text in paragraph 1</div>
<div>Some text in paragraph 1</div>
</div><br>
<div id="a2">
<div id="3">Some text in paragraph 2</div>
<div>Some text in paragraph 1</div>
</div><br>
<div id="3">
<div>Some text in paragraph 3</div>
<div>Some text in paragraph 1</div>
</div><br>
</div>
Upvotes: 1
Reputation: 16
$('#main>div').not('#2').each(function() {
$(this).css('background-color', 'blue');
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div id="main">
<div id="1">
<div>Some text in paragraph 1</div>
<div>Some text in paragraph 1</div>
</div><br>
<div id="2">
<div id="3">Some text in paragraph 2</div>
<div>Some text in paragraph 1</div>
</div><br>
<div id="3">
<div>Some text in paragraph 3</div>
<div>Some text in paragraph 1</div>
</div><br>
</div>
Upvotes: 0
Reputation: 417
You have to exclude div#2 in this way:
$('#main > div').not('#2').each(function() {
$(this).css('background-color', 'blue');
});
Or more simply:
$('#main > div').not('#2').css('background-color', 'blue');
Pure CSS:
#main > div:not(#2) {
background-color: blue;
}
Upvotes: 2