jQuery - Select everything on a page except one di

2019-06-13 18:14发布

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.

2条回答
爱情/是我丢掉的垃圾
2楼-- · 2019-06-13 18:43

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.

查看更多
祖国的老花朵
3楼-- · 2019-06-13 18:55

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');
;
查看更多
登录 后发表回答