Domanda

Voglio solo un cassetto per essere aperto all'avvio. Al momento, tutti i cassetti sono aperti.

La codice jQuery

$(document).ready(function () {
    // hide all ULs inside LI.drawer except the first one
    $('LI.drawer UL:not(:first)').hide();

    // apply the open class
    $('li.drawer:first ul').addClass('open');

    $('h2.drawer-handle').click(function () {
        // hide the currently visible drawer contents
        $('li.drawer ul:visible').hide();

        // remove the open class from the currently open drawer
        $('h2.open').removeClass('open');

        // show the associated drawer content to 'this' (this is the current H2 element)
        // since the drawer content is the next element after the clicked H2, we find
        // it and show it using this:
        $(this).next().show();

        // set a class indicating on the H2 that the drawer is open
        $(this).addClass('open');
    });
});

Il codice HTML nel corpo

<ul class="drawers">

   <li class="drawer">
      <h2 class="drawer-handle open">Contact</h2>
      <ul>
           <li>A</li>
           <li>B</li>
      </ul>
   </li>

   <li class="drawer">
      <h2 class="drawer-handle">papers</h2>
      <ul>
           <li>A</li>
           <li>B</li>
      </ul>
   </li>

</ul>

Come si può mostrare un cassetto e nascondere il resto all'avvio?

È stato utile?

Soluzione

Hai avuto l'idea giusta, solo il posizionamento sbagliato del vostro:. Prima clausola

$('li.drawer:first ul').addClass('open');

Altri suggerimenti

Il codice originale funziona troppo.

ho avuto il seguente errore nel mio codice sul server

$(document).ready(function () {
// hide all ULs inside LI.drawer except the first one $('LI.drawer UL:not(:first)').hide();

...

La prima riga del mio codice era sotto il segno di commento.

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top