Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

259
Vistas
Alternating color rows based on values of cells

I'd like to have a table with alternating colors of rows but rows with the same value should have unitary background color. Can I achieve such an effect with CSS?

Example: enter image description here

about 4 years ago · Juan Pablo Isaza
1 Respuestas
Responde la pregunta

0

JQuery (feel free to simplify and edit this)

$(function() {
   let elems = $('tr:not(".head")');
   let count = 0;
  
   for (let i=0; i<elems.length; i++) {
      let el = $(elems[i]),
          prev = $(elems[i-1]);
      
      let isEven = (count % 2 == 0);
      
      if (isEven) {
         el.css('background', 'red');
      } else {
         el.css('background', 'yellow');
      }

      if (el.text() == prev.text()) {
         let clr = prev.css('background');
         el.css('background', clr);
      } else {
        count++;
      }
   }
 });
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<table>
<tr class="head">
  <th>One</th>
  <th>Two</th>
</tr>
<tr>
  <td>1</td>
  <td>1</td>
</tr>
<tr>
  <td>1</td>
  <td>1</td>
</tr>
<tr>
  <td>2</td>
  <td>2</td>
</tr>
<tr>
  <td>3</td>
  <td>3</td>
</tr>
<tr>
  <td>3</td>
  <td>3</td>
</tr>
<tr>
  <td>3</td>
  <td>3</td>
</tr>
<tr>
  <td>4</td>
  <td>4</td>
</tr>
<tr>
  <td>5</td>
  <td>5</td>
</tr>
<tr>
  <td>6</td>
  <td>6</td>
</tr>
<tr>
  <td>7</td>
  <td>7</td>
</tr>
<tr>
  <td>8</td>
  <td>8</td>
</tr>
<tr>
  <td>8</td>
  <td>8</td>
</tr>
<tr>
  <td>8</td>
  <td>8</td>
</tr>
  
</table>

about 4 years ago · Juan Pablo Isaza Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda