简体   繁体   中英

jquery select all LI from UL's, that not in UL element with class

Is it possible to select all elements from ul , exclude ul with class not-need ?

<ul>
    <li></li>
    <li></li>
    <li></li>
    <li></li>
    <li></li>
</ul>
<ul class='not-need'>
    <li></li>
    <li></li>
    <li></li>
    <li></li>
    <li></li>
</ul>

You can use :not() or not() for excluding element

 $('ul:not(.not-need) li').css('color', 'red'); 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script> <ul> <li>1</li> <li>2</li> <li>3</li> <li>4</li> <li>5</li> </ul> <ul class='not-need'> <li>1</li> <li>2</li> <li>3</li> <li>4</li> <li>5</li> </ul> 

or

 $('ul').not('.not-need').find('li').css('color', 'red'); 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script> <ul> <li>1</li> <li>2</li> <li>3</li> <li>4</li> <li>5</li> </ul> <ul class='not-need'> <li>1</li> <li>2</li> <li>3</li> <li>4</li> <li>5</li> </ul> 

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM