Невозможно показать только один ящик от jQuery при запуске - PullRequest
0 голосов
/ 15 апреля 2009

Я хочу, чтобы при запуске был открыт только один ящик. На данный момент все ящики открыты.

Код 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>

Как показать один ящик и спрятать остальные при запуске?

Ответы [ 2 ]

1 голос
/ 15 апреля 2009

У вас правильная идея, просто неправильное размещение вашего: first условия.

$('li.drawer:first ul').addClass('open');
0 голосов
/ 16 апреля 2009

Оригинальный код тоже работает.

У меня была следующая ошибка в моем коде на сервере

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

...

Первая строка моего кода была под отметкой комментария.

...