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

162
Visualizações
cant remove class from menu item

I have a dropdown menu. On some pages, the link just scrolls to the anchor instead of refreshing the page. When this happens then dropdown menu is supposed to close. But it does not.

This is my html

<div class="navbar">
    <div class="nav_item">
        <div class="dropdown menu_item" >
            <span class="navbar-submenu"> <a href="/">dropdown</a></span>
            <div class="secondarynav" >
                <div class="nav_item">
                    <div class="menu_item" >
                        <a href="/#">link</a>
                    </div>
                </div>
                <div class="nav_item">
                    <div class="menu_item" >
                        <a href="/#">link</a>
                    </div>
                </div>
            </div>

        </div>
    </div>
    <div class="nav_item">
        <div class="menu_item" >
            <a href="/">link</a>
        </div>
    </div>
</div>

What is supposed to happen is when a visitor clicks on dropdown, active class is added to the parent nav_item, which then shows the rest of the menu. When a user clicks dropdown again active is removed and dropdown closes.

$('.dropdown').click(function (e) {
    $(this).closest('.nav_item').toggleClass('active');
});

Now the problem, when a visitor clicks dropdown, then any of the children links I would like the active class removed, which currently only happens when the dropdown link is clicked.

I have tried various other methods

$('.nav_item.active > *').click(function(e) {
    $(this).closest('.active').removeClass('active');
});

But this glitches with the toggle function in the other scipt. Basically the active is added then removed straight away.

How can I set it so when .dropdown is clicked .active is added to the parent nav_item. But then if any link in that menu is clicked the .active is removed? Thanks

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

0

Use e.stopPropagation() to prevent the event on the item from bubbling up to the dropdown.

$('.nav_item.active > *').click(function(e) {
    e.stopPropagation();
    $(this).closest('.active').removeClass('active');
});
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