غير قادر على إظهار درج واحد فقط بواسطة jQuery عند بدء التشغيل

StackOverflow https://stackoverflow.com/questions/749660

  •  09-09-2019
  •  | 
  •  

سؤال

أريد أن يكون درج واحد فقط مفتوحا عند بدء التشغيل. في الوقت الحالي، جميع الأدراج مفتوحة.

ال رمز مسج

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

أتش تي أم أل في الجسم

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

كيف يمكنك إظهار درج واحد وإخفاء الباقي عند بدء التشغيل؟

هل كانت مفيدة؟

المحلول

لديك الفكرة الصحيحة، مجرد موضع خاطئ من البند الأول الخاص بك.

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

نصائح أخرى

الكود الأصلي يعمل أيضا.

كان لدي خطأ ما بعد في التعليمات البرمجية على الخادم

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

...

الخط الأول من التعليمات البرمجية كان تحت علامة التعليق.

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top