Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

193
Visualizações
jQuery Número automático basado en la clase div

Quiero usar jQuery para asignar un número a cada fila. Si el elemento tiene una clase oculta, ignórelo y ordene automáticamente el número.

 <style> :before{ display:block; margin-top:10px; font: inherit; } .tc-hidden{ display:none; } .tc1 h3.tm-section-label:before { content: "1"; } .tc2 h3.tm-section-label:before { content: "2"; } .tc3 h3.tm-section-label:before { content: "3"; } .tc4 h3.tm-section-label:before { content: "4"; } </style>
 <div class="tc-cell tc1"> <h3 class="tm-section-label"></h3> </div> <div class="tc-cell tc2 tc-hidden"> <h3 class="tm-section-label"></h3> </div> <div class="tc-cell tc3 tc-hidden"> <h3 class="tm-section-label"></h3> </div> <div class="tc-cell tc4"> <h3 class="tm-section-label"></h3> </div>

Como se sabe, el último elemento debe mostrar el número 2 pero muestra el número 4

about 4 years ago · Juan Pablo Isaza
2 Respostas
Responde à pergunta

0

Puede eliminar clases de tc1 tc2 tc3 tc4 y si cada elemento no tiene tc-hidden , agregue la Clase apropiada.

 var count=0; $.each($(".tc-cell"),(index,item)=>{ if(!$(item).hasClass("tc-hidden")) $(item).addClass("tc" + ((index+1)-count)); else count++; });
 <style> :before{ display:block; margin-top:10px; font: inherit; } .tc-hidden{ display:none; } .tc1 h3.tm-section-label:before { content: "1"; } .tc2 h3.tm-section-label:before { content: "2"; } .tc3 h3.tm-section-label:before { content: "3"; } .tc4 h3.tm-section-label:before { content: "4"; } </style>
 <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> <div class="tc-cell "> <h3 class="tm-section-label"></h3> </div> <div class="tc-cell tc-hidden"> <h3 class="tm-section-label"></h3> </div> <div class="tc-cell tc-hidden"> <h3 class="tm-section-label"></h3> </div> <div class="tc-cell"> <h3 class="tm-section-label"></h3> </div>

about 4 years ago · Juan Pablo Isaza Relatório

0

Puede recorrer los elementos .tc-cell y determinar si estaban ocultos y ajustar dinámicamente sus nombres de clase tc-<n> :

 let i = 1; // Starting index number for tc<i> class name to use $('.tc-cell').each(function(){ // Remove all tc-<n> classes from this tc-cell // See https://stackoverflow.com/a/5182103/378779 $(this).removeClass (function (index, className) { return (className.match (/(^|\s)tc\d+/g) || []).join(' '); }); // If we should not be hidden, add a class of tc<i> and increment i: if ( ! $(this).hasClass('tc-hidden') ) { $(this).addClass('tc' + i++); } });
 :before{ display:block; margin-top:10px; font: inherit; } .tc-hidden{ display:none; } .tc1 h3.tm-section-label:before { content: "1"; } .tc2 h3.tm-section-label:before { content: "2"; } .tc3 h3.tm-section-label:before { content: "3"; } .tc4 h3.tm-section-label:before { content: "4"; }
 <script src="https://code.jquery.com/jquery-3.6.0.js" integrity="sha256-H+K7U5CnXl1h5ywQfKtSj8PCmoN9aaq30gDh27Xc0jk=" crossorigin="anonymous"></script> <div class="tc-cell tc1"> <h3 class="tm-section-label"></h3> </div> <div class="tc-cell tc2 tc-hidden"> <h3 class="tm-section-label"></h3> </div> <div class="tc-cell tc3 tc-hidden"> <h3 class="tm-section-label"></h3> </div> <div class="tc-cell tc4"> <h3 class="tm-section-label"></h3> </div>

about 4 years ago · Juan Pablo Isaza Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda