我试图用jQuery切换div以显示隐藏子项。我正在尝试在切换打开事件和关闭事件上向父项添加类。我们如何跟踪切换事件?
Here is fiddle

$("#flip").click(function(){
     $("#panel").toggle();
 });

$("#panel").toggle(
  function() {
    alert("first event");
  }, function() {
    alert("second event");
  });

最佳答案

您可以在调用complete时指定slideToggle()回调,在其中可以确定元素是向上滑动还是向下滑动。

$("#flip").click(function(){
    $("#panel").slideToggle(function() {
        if($(this).is(':visible')) {
            alert('Panel is visible');
            // code to add class. "this" refers to the panel here
        }
        else {
            alert('Panel is not visible');
            // code to remove class. "this" refers to the panel here
        }
    });
});


更新了fiddle

10-07 13:06
查看更多