8

This code shows frm01:

$(document).ready(function() {
$("#reg").click(function () {
$("#frm01").show("slide", { direction: "down" }, 1000);
});
});

But I want to hide frm01 if it is allready visible, and vice versa. How could I do this, please ?

4

4 回答 4

14

Try jQuery's toggle() method:

$(function() {
    $("#reg").click(function () {
        $("#frm01").toggle(1000);
    });
});

You don't need jQuery to use if-else statements; Javascript implements those natively...

$(function() {
    $("#reg").click(function () {
        if ($("#frm01").is(":visible"))
            $("#frm01").slideUp(1000);
        else
            $("#frm01").slideDown(1000);
    });
});
于 2012-07-15T21:24:50.867 回答
2

try this:

$(document).ready(function() {
   $("#reg").click(function () {
    if ($("#frm01").is(':hidden')) {
      $("#frm01").show("slide", { direction: "down" }, 1000);
    } else {
      $("#frm01").hide(1000);
    }
  });
});
于 2012-07-15T21:26:17.313 回答
1
  $("#reg").on('click', function () {
    $("#frm01:hidden").show("slide", { direction: "down" }, 1000);
    $("#frm01:visible").hide("slide", { direction: "up" }, 1000);
  });

或者

  $("#reg").toggle(
    function () {
      $("#frm01").show("slide", { direction: "down" }, 1000);
    },
    function(){
      $("#frm01").hide("slide", { direction: "up" }, 1000);
    }
  );

也应该工作

于 2012-07-15T21:26:59.473 回答
1

您可以使用 $.toggle() 方法来显示和隐藏。需要一些时间来确定元素是隐藏还是显示。

$("#reg").click(function () {
if($("#frm01").hasClass('fram1-slide-on')){
    $("#frm01").hide();
    $("#frm01").removeClass('fram1-slide-on');
}   else {
    $("#frm01").show("slide", { direction: "down" }, 1000);
    $("#frm01").addClass('fram1-slide-on');
}

});

于 2012-07-15T21:56:46.110 回答