I have been desperately trying to figure out why .not() will not work in this case
link: http://jsfiddle.net/Kr8SA/4/
Thank you for your help.
$('#site').not('#box') .fadeIn();
#site { display: none; } div { border: 2px solid black; width: 300px; height: 300px; } #box { background: gray; }
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <div id="site"> <div id="box"></div> <div></div> </div>
The .not
function does not search within all the descendants of the given set. It filters the elements from the set. Since $('#site').length == 1
, it won't work.
Two alternatives would be
$('#site *').not('#box')
$('#site').find('*:not(#box)')
$('#site *').not('#box').fadeIn()
/* Hide the elements we'll fade in */ #site *{ display: none; } div { border: 2px solid black; width: 300px; height: 300px; } #box { background: gray; }
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <div id="site"> <div id="box"></div> <div>Something to show</div> </div>
.not()
works as an exclusion filter the current query result, not on children or other descendents. Your query only contains #site
. So telling jQuery to exclude #box
using .not()
will do nothing.
Your query reads like:
#site
, our query result only contains #site
now #box
, our query doesn't contain #box
so don't do anything #site
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.