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

87
Visualizações
How to add a class to currently visiting page and the parent directory for styling

How can I add a class to the current page and the parent directory page or folder, so I can apply CSS styles?

For example:

root
|- Products
  |- Notebook
  |- Pencil
  |- Bags

Currently visiting: www.domain.com/products/notebooks

<a href="/products" class="current"> Product </a>
<a href="/products/notebook" class="current"> Notebook </a>
<a href="/products/pencil"> Pencil </a>
<a href="/products/bags"> Bags </a>

Result:

Products
Notebooks || Pencil || Bags

I am trying something like this but not success.

push(function() {
  var curUrl = location.pathname;
  $('a').each(function() {
    $(this).toggleClass('current', curUrl.indexOf($(this).attr('href')) != -1);
  });
about 4 years ago · Santiago Trujillo
1 Respostas
Responde à pergunta

0

Finding URL in location works, see snippet below (not using location.pathname just for the snippet, of course keep it in your code).

The push(function() { ... surrounding the function is maybe not needed. Just wait for document ready.

var curUrl = 'https://example.com/shop/products/notebook'; //location.pathname;
$('a').each(function() {
  $(this).toggleClass('current', curUrl.indexOf($(this).attr('href')) != -1);
});
* { margin: 0; padding; 0; }
a { font-size: 14px; text-decoration: none; margin: 4px; }
a.current { font-weight: bold; }
<a href="/products"> Product </a>
<a href="/products/notebook"> Notebook </a>
<a href="/products/pencil"> Pencil </a>
<a href="/products/bags"> Bags </a>

<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>

about 4 years ago · Santiago Trujillo 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