0

我只想在启动时打开一个抽屉。此刻,所有抽屉都打开了。

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>

您如何在启动时显示一个抽屉并隐藏其余抽屉?

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