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

212
Visualizações
Bootstrap v3 Navbar-toggle with multiple data-targets don't toggle class "collapsed"

The data-target attribute accepts multiple CSS selectors to apply the collapse to. If only one selector is used, the class "collapsed" of navbar-toggle is toggling by uncollapsing. But if multiple selectors are used, this behavior don't works. In this case class "collapsed" is always present. But I need the change of class "collapsed" in navbar-toggle.

It would be great if someone could help.

<div id="mainNav">
<nav class="navbar navbar-inverse">
    <div class="navbar-header">
        <button type="button" class="navbar-toggle collapsed" data-toggle="collapse" data-target="#navbar, #userPanel" aria-expanded="false" aria-controls="navbar">
            <span class="sr-only">Toggle navigation</span>
            <span class="icon-bar top-bar"></span>
            <span class="icon-bar middle-bar"></span>
            <span class="icon-bar bottom-bar"></span>
        </button>
    </div>

    <div id="navbar" class="navbar-collapse collapse">
        <ul class="nav navbar-nav">
            <li class="active">
                <a href="#">
                    <span class="navicon glyphicon glyphicon-th" aria-hidden="true"></span>
                    <span class="navtext">Dashboard</span>
                </a>
            </li>
            <li>
                <a href="#">
                    <span class="navicon glyphicon glyphicon-fire" aria-hidden="true"></span>
                    <span class="navtext">About</span>
                </a>
            </li>
        </ul>
    </div>
</nav>
</div>

<div id="userPanel" class="navbar-collapse collapse">
<ul>
    <li>Max Muster | 101945</li>
    <li><a href="#"><span class="glyphicon glyphicon-off" aria-hidden="true"></span> Logout</a></li>
</ul>

about 4 years ago · Santiago Trujillo
3 Respostas
Responde à pergunta

0

I found a solution without restructuring the elements:

I used class selector (".navbar-collapse") as the data-target, it worked fine but I faced the same issue with the toggle button. Namely the "collapsed" class was not removed - the script could not find the trigger element when multiple targets were specified.

According to the source code on GitHub they lookup for the trigger element as the following:

this.$trigger = $('[data-toggle="collapse"][href="#' + element.id + '"],' +
                  '[data-toggle="collapse"][data-target="#' + element.id + '"]')

Obviously it won't work if we use a coma separated list of element IDs or class selector in the data-target property. It will iterate through all elements, but won't find the matching trigger button. Luckily there is a trick I just found. The trigger selector checks both the data-target and the href attributes (using OR logical operator), so we can cheat it by adding a href attribute to the the toggle button, pointing to one of the existing IDs (#navbar OR #userPanel) in my sample code I used the first one:

So the solution without restructuring is:

<button type="button" class="navbar-toggle collapsed" data-toggle="collapse" data-target=".navbar-collapse" href="#navbar" aria-expanded="false" aria-controls="navbar,userPanel">
    ...
</button>

Only changing the trigger button's attributes, the rest of the code remains the same.

about 4 years ago · Santiago Trujillo Relatório

0

wrap both in a div with a id and use class for both data's as given code try using data-parent to collapse both , and let me know is it working for you,

<div id="wraper"> 
   <div id="mainNav">
      <nav class="navbar navbar-inverse">
        <div class="navbar-header">
            <button type="button" class="navbar-toggle collapsed" data-toggle="collapse" data-parent="#wraper" data-target=".navbar_collapsed_data" aria-expanded="false" aria-controls="navbar">
                <span class="sr-only">Toggle navigation</span>
                <span class="icon-bar top-bar"></span>
                <span class="icon-bar middle-bar"></span>
                <span class="icon-bar bottom-bar"></span>
            </button>
        </div>

        <div class="navbar_collapsed_data navbar-collapse collapse">
            <ul class="nav navbar-nav">
                <li class="active">
                    <a href="#">
                        <span class="navicon glyphicon glyphicon-th" aria-hidden="true"></span>
                        <span class="navtext">Dashboard</span>
                    </a>
                </li>
                <li>
                    <a href="#">
                        <span class="navicon glyphicon glyphicon-fire" aria-hidden="true"></span>
                        <span class="navtext">About</span>
                    </a>
                </li>
            </ul>
        </div>
      </nav>
    </div>

<div class="navbar_collapsed_data navbar-collapse collapse">
    <ul>
        <li>Max Muster | 101945</li>
        <li><a href="#"><span class="glyphicon glyphicon-off" aria-hidden="true"></span> Logout</a></li>
    </ul>
  </div>

about 4 years ago · Santiago Trujillo Relatório

0

Now I have made a small restructuring and wrapped the collapse elements in one container. In my case it works:

<nav class="navbar navbar-inverse">
   <div class="navbar-header">
      <button type="button" class="navbar-toggle collapsed" data-target="#collapseArea" data-toggle="collapse" aria-expanded="false" aria-controls="navbar">
       ...
      </button>
   </div>

   <div id="collapseArea" class="navbar-collapse collapse">
       <ul id="mainNav" class="nav navbar-nav">
          <li>...</li>
          <li>...</li>
       </ul>

       <div id="userPanel">
           <ul>
               <li>...</li>
               <li>...</li>
           </ul>
       </div>
   </div>
</nav>
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