Pregunta

$(function() {
    $("table.section thead").click(function() {
      if ($(this).next("table.section tbody").style.display == "block"){
         $(this).next("table.section tbody").slideUp("slow");
      }
      if ($(this).next("table.section tbody").style.display == "none"){
         $(this).next("table.section tbody").slideDown("slow");
      }
    });
});

No sé cómo lograr esto, cualquier ayuda sería muy apreciada.

ACTUALIZACIÓN:

Yo estaba tratando de utilizar la siguiente función.

$(function() {
    $("table.section thead").click(function() {
    $(this).next("table.section tbody").slideToggle("slow");

    });
});

Lo que me da un problema, (cuando THEAD que se derrumbe y hace clic en él, se expande y luego se comprima de nuevo, por lo que siempre sería colapso). Es por eso que estoy tratando de conseguir la función en la parte superior.

¿Fue útil?

Solución

Utilice el visible en este caso:

$(function() {
    $("table.section thead").click(function() {
      var body = $(this).next("table.section tbody");
      if (body.is(":visible"))
         body.slideUp("slow");
      else
         body.slideDown("slow");
    });
});

Pero, probablemente, mucho más simple sólo tiene que utilizar .slideToggle() como esto:

$(function() {
    $("table.section thead").click(function() {
      $(this).next("table.section tbody").slideToggle("slow");
    });
});
Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top