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

263
Visualizações
jQuery addClass to a child element not working in Egde

I have this structure:

<ul>
  <li class="js-drilldown-back"><a>Back</a></li>
  <li><a href="#">item</a></li>
  <li><a href="#">item</a></li>
  <li><a href="#">item</a></li>
</ul>

and I'm trying to add a class to the first tag, so it becomes this:

<ul>
  <li class="js-drilldown-back"><a class="click-back">Back<a></li>
  <li><a href="#">item</a></li>
  <li><a href="#">item</a></li>
  <li><a href="#">item</a></li>
</ul>

The framework I use is adding this back button dynamically, so I can't manually add the class to it. I have tried this code:

$(".js-drilldown-back").children("a").addClass("click-back")

but it doesn't seem to work in Edge, other browsers seem happy with it. I've also tried:

$(".js-drilldown-back:has a").addClass("click-back")
$(".js-drilldown-back:has > a").addClass("click-back")
$(".js-drilldown-back a").addClass("click-back")
$(".js-drilldown-back > a").addClass("click-back")

What I'm trying to achieve is when you click the Back button it removes a class from the body tag, ie:

$(".click-back").click(function(){
  $("#page").removeClass("submenu-open")
})

Targetting the parent (.js-drilldown-back) doesn't seem to work which is why I'm trying to add the class to the child and target this instead.

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

0

You don't appear to have proper semicolon terminations after each statement.

$(".js-drilldown-back").children("a").addClass("click-back");

:first-child seems to work fine in Edge, or maybe the console.log in jsfiddle is being lenient? I also reworked how you're applying your on click event and confirmed that it removes the class from the anchor.

$(".js-drilldown-back:first-child").addClass("click-back");
console.log( $(".js-drilldown-back:first-child").hasClass("click-back") );

$(document).on("click", ".click-back", function(){
  $(this).removeClass("click-back");
})

Console confirms that the anchor has the desired class, i.e., :first-child is working.

Console confirms that the anchor has the desired class

Upon clicking the anchor, source confirms that the class is removed. I changed the target just to confirm it was removing something I could see and verify.

Upon clicking the anchor, source confirms that the class is removed

All tested in Edge here: https://jsfiddle.net/8bfo9cza/

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