问题
How can I select each and everything on a page except a particular div using jQuery?
Actually I am trying to fade the rest of background on popup of a particular div. But the problem is that not everything is inside some div. Few elements don't have any parent div.
<body>
12345
<div id='second_div'>
XXXXXX
</div>
56789
<div id='popup'>
AAAAA
</div>
</body>
I am using code below but this is not fading content which don't have a parent div(i.e 12345 and 56789). It is only fading content of 'second_div'(i.e. XXXXXX ).
$('body > div:not(#popup)').css("opacity",'0.7');
Please guide me on how to do this using jQuery.
回答1:
I think you are looking for this:
$("body > *").not('#popup').css("opacity", '0.7');
The reason this works is because it selects all the direct descendants of body
and then removes all direct descendants that have an id
of popup
.
回答2:
Tested it and works perfectly, although I've tested the other one and it doesn't but anyway, good luck!
var lines = $('body').html().split('\n');
$.each(lines, function(k, v){
lines[k] = '<span>'+v+'</span>';
});
$('body').html(lines.join(''));
$('body > *').css("opacity", '0.7').find('#popup').parent().css("opacity", '1');
;
来源:https://stackoverflow.com/questions/10506117/jquery-select-everything-on-a-page-except-one-div