No se pueden mostrar sólo un cajón por jQuery en el arranque
-
09-09-2019 - |
Pregunta
Quiero sólo un cajón sea abierto en el inicio. Por el momento, todos los cajones están abiertos.
$(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');
});
});
El HTML en el cuerpo
<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>
¿Cómo se puede mostrar un cajón y ocultar el resto en el arranque?
Solución
Usted tiene la idea correcta, sólo la colocación incorrecta de la:. Primera cláusula
$('li.drawer:first ul').addClass('open');
Otros consejos
El código original también funciona.
Yo tenía el siguiente error en mi código en el servidor
$(document).ready(function () {
// hide all ULs inside LI.drawer except the first one $('LI.drawer UL:not(:first)').hide();
...
La primera línea de mi código era bajo la marca de comentario.
Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow