I'm going to hide all the elements of the sample except the first. There is the following house structure:

<div id="_result"> <div><!--оставить в живых этот блок--> <div> <div>...</div> </div> </div><!-- оставить в живых этот блок--> <p>..</p> <ul>....</ul> <p>..</p> <ul>..</ul> <div></div> </div> 

I write the following

 $("#_result:not(:eq(0))").hide(); 

but does not work, how to fix?

  • add space $("#_result :not(:eq(0))").hide(); - Grundy
  • @Grundy, not everything is dropping jsfiddle.net/c8qeufuh/1 - vasily_dumov
  • not really everything :) - Grundy

1 answer 1

In this type of "#_result:not(:eq(0))" expression will find all elements with id = _result that are not the first in the sample, since there is only one such element, it is not included in the sample.

To solve, you need to look for not the first element among children, for this you need to add a sign >

"#_result>:not(:eq(0))"

For example:

 $("#_result>:not(:eq(0))").hide(); 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <div id="_result"> <div> <!--оставить в живых этот блок--> <div> <div>я должен быть виден</div> </div> </div> <!-- оставить в живых этот блок--> <p>а я нет</p> <ul>и я нет</ul> <p>и я</p> <ul>и я</ul> <div>и даже я</div> </div> 

You can also go the other way and choose what you need to leave in the code

 $("#_result").children().slice(1).hide(); 

Example

 $("#_result").children().slice(1).hide(); 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <div id="_result"> <div> <!--оставить в живых этот блок--> <div> <div>я должен быть виден</div> </div> </div> <!-- оставить в живых этот блок--> <p>а я нет</p> <ul>и я нет</ul> <p>и я</p> <ul>и я</ul> <div>и даже я</div> </div> 

  • if you need to leave the first and the last element then $ ("#_ result>: not (: eq (0)): not (: last)"). hide (); - vasily_dumov
  • @vasily_dumov, it’s better not to give code examples in the comments, usually it’s too unreadable, and if the selector becomes complicated, it’s easier to choose through the code, rather than trying to find a selector that fits - Grundy