xxxxx...">

Many jquery selectors with AND OR clause

If I have a table with 4000 rows

<table> <tr class="abcdefg"><td>xxx</td></tr> <tr class="acdefg"><td>xxx</td></tr> <tr class="defg"><td>xxx</td></tr> <tr class="adefg"><td>xxx</td></tr> . . . . . </table> 

If I want to select strings that have classes (a or b or c) and (d or e) and (a or g)

How can I encode a selector operator?

thanks

+4
source share
3 answers

Try the following:

 $('.a, .b, .c').filter('.d, .e').filter('.a, .g') 
+11
source

Firstly, 4,000 lines seem a bit overkill and will be slow no matter how you think about it. If you just want to use jQuery to select all rows based on multiple class types, just use the multiple selector syntax:

 $('.a, .b, .c')... 

This will select all elements of class a, b or c. From there, you can use the $ .filter () function to further filter the list with other types of classes:

 $('.a, .b, .c').filter('.d, .e')... 

This will start by selecting all elements with classes a, b or c, then everything that does not contain class d, or e will be filtered. You can filter as many times as you want, once you reach your final list, you can repeat all the results using the $ .each () function:

 $result.each(function() { do stuff... }); 
0
source

Try the following:

 $(document).ready(function() { var items = $('.a,.b,.c').filter(function() { var classes = $(this).prop('class').split(' '); return (classes.indexOf('d') > 0 || classes.indexOf('e') > 0) && (classes.indexOf('a') > 0 || classes.indexOf('g') > 0) }); items.find('td').css('color', 'red'); }); 

See here: http://jsfiddle.net/UcYnu/

0
source

Source: https://habr.com/ru/post/1492137/


All Articles