Frage

Ich will nur eine Schublade beim Start geöffnet sein. Im Moment sind alle Schubladen offen.

Die jQuery-Code

$(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');
    });
});

Der HTML-Code in dem Körper

<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>

Wie kann man eine Schublade zeigen und den Rest beim Start verstecken?

War es hilfreich?

Lösung

Sie haben die richtige Idee, nur die falsche Platzierung Ihrer. Erste Klausel

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

Andere Tipps

Der ursprüngliche Code funktioniert auch.

Ich hatte die folgenden Fehler in meinem Code auf dem Server

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

...

Die erste Zeile meines Codes war unter dem Kommentarzeichen.

Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top