0

起動時に引き出しを1つだけ開いてほしい。現在、すべての引き出しが開いています。

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

本文のHTML

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

起動時に1つの引き出しを表示し、残りを非表示にするにはどうすればよいですか?

4

2 に答える 2

1

あなたは正しい考えを持っています、あなたの:first節のちょうど間違った配置。

$('li.drawer:first ul').addClass('open');
于 2009-04-14T23:08:29.107 に答える
0

元のコードも機能します。

サーバーのコードに次のバグがありました

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

...

私のコードの最初の行はコメントマークの下にありました。

于 2009-04-16T13:56:09.690 に答える