I am working on the following code. How can I hide only the divs which has not the classes as indicated in the mopt[]
?
As you can see I am trying to show only two divs which has the Q
and M
classes but my code in hiding all of divs
$('.AWB').css("background-color", "red"); let mopt = ['Q','M'] for (i = 0; i < mopt.length; i++) { $(".box:not(:has(" + mopt[i] + "))").hide() }
.box { height: 20px; background: khaki; width: 100px; text-align:center; }
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> <div class="box ABFRWQ">Has Q</div> <div class="box ABFHKF">No Q</div> <div class="box ABFWR">No Q</div> <div class="box ABFHKF">No Q</div> <div class="box ABFWRM">Has M</div> <div class="box ABFHKF">No Q</div> <div class="box ABQFHMKF">Has Q & M</div>
you can simply use this code..
$('.AWB').css("background-color", "red"); $(".box").not(".Q,.M").hide();
<!DOCTYPE html> <html> <head> <title>demo</title> <style type="text/css"> .box { height: 20px; background: khaki; width: 100px; text-align:center; } </style> </head> <body> <div class="box ABFRWQ">Has Q</div> <div class="box ABFHKF">No Q</div> <div class="box ABFWR">No Q</div> <div class="box ABFHKF">No Q</div> <div class="box ABFWRM">Has M</div> <div class="box ABFHKF">No Q</div> <div class="box ABQFHMKF">Has Q & M</div> <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> <script type="text/javascript" src="app.js"></script> </body> </html>
You're looking to hide boxes that have neither Q
nor M
, so you can filter on those that do not have .Q, .M
, and hide those.
$('.AWB').css("background-color", "red"); let mopt = ['Q','M']; $('.box').not( mopt.map(function(className){ return '.'+ className; }).join(', ') ).hide();
.box { height: 20px; background: khaki; width: 100px; text-align:center; }
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> <div class="box ABFRWQ">Has Q</div> <div class="box ABFHKF">No Q</div> <div class="box ABFWR">No Q</div> <div class="box ABFHKF">No Q</div> <div class="box ABFWRM">Has M</div> <div class="box ABFHKF">No Q</div> <div class="box ABQFHMKF">Has Q & M</div>
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.