简体   繁体   English

启动时无法仅由jQuery显示一个抽屉

[英]Unable to show only one drawer by jQuery at startup

I want only one drawer to be open at startup. 我只希望在启动时打开一个抽屉。 At the moment, all drawers are open. 目前,所有抽屉均已打开。

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

The HTML in the body 正文中的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>

How can you show one drawer and hide the rest at the startup? 如何在启动时显示一个抽屉并将其余的隐藏?

您有正确的主意,只是:first子句的位置错误。

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

The original code works too. 原始代码也可以。

I had the following bug in my code at the server 我在服务器上的代码中存在以下错误

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

...

The first line of my code was under the comment mark. 我的代码的第一行在注释标记下。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM