CSS selector finds subset of entities based on class filter
P粉293550575
P粉293550575 2023-09-06 23:21:53
0
1
504

I have a table that contains a set of rows represented, and within that set of tables, some rows have a "clickable" category. What I'm trying to do is apply a style to every odd row that has a "clickable" category. I've tried various selectors, but it only seems to apply the style to clickable odd rows, which are odd when applied to all table rows, not just those with the "clickable" class. I think I need the selector to find a subset of rows that have the "clickable" class and then iterate over those rows for the odd number of children.

If rows 1, 4, 5, 7 and 8 are clickable, then I want rows 4 and 7 to be considered the odd rows here. However, it seems to look at 1(c),2,3,4(c),5(c),6,7(c),8(c),9,10 and when looking for odd rows with clickable (Using c as reference) Only 4, 8 are selected since they are at odd positions in the entire set of rows.

I tried a lot of selectors but no luck. I'm hoping this is a possible scenario and someone can help me with some feedback or a solution if possible.

td { border-bottom: 1px solid #cdcdcd; width: 100px; } .clickable { font-weight: 600; font-size: 16px; } .clickable:nth-child(odd) { background-color: lightcoral; }
Dean Canada True
Fred Canada True
Sam Canada True
Gina Canada True
Sam Canada True
Alex Canada True
Eli Canada True
Emma Canada True
John Canada True
Sophie Canada True
Sarah Canada True

P粉293550575
P粉293550575

reply all (1)
P粉190883225

This is what you are looking for:

tr:nth-child(even of .clickable) { background-color: lightcoral; }

The answer source comes fromhere

Or use java script/jquery for broad browser support:

$('tr.clickable').each(function(index, value) { if(index % 2 != 0) $(value).addClass('custom-bg'); });
td { border-bottom: 1px solid #cdcdcd; width: 100px; } .clickable { font-weight: 600; font-size: 16px; } .clickable.custom-bg { background-color: lightcoral; }
 
Dean Canada True
Fred Canada True
Sam Canada False
Gina Canada False
Sam Canada True
Alex Canada False
Eli Canada True
Emma Canada True
John Canada True
Sophie Canada False
Sarah Canada True
    Latest Downloads
    More>
    Web Effects
    Website Source Code
    Website Materials
    Front End Template
    About us Disclaimer Sitemap
    php.cn:Public welfare online PHP training,Help PHP learners grow quickly!