2012-07-23 78 views
0

我有这样的jQuery代码:jQuery的切换不能正常工作

$('.item_bar_action_holder_drop a').click(function() { 
      var item_bar = $('.item_bar_action_holder_actions'); 
      if(item_bar.is(':visible')) { 
       $(this).removeClass('active'); 
      } else { 
       $(this).addClass('active'); 
      } 
      item_bar.slideToggle('fast', function() { 
      }); 
     }); 

这里的问题是,当我点击多少次按钮“活动”类将设定为关闭面板和它的错。我不明白为什么它发生。

+0

你只有1个面板吧? – 2012-07-23 10:50:40

+0

你能告诉我们你的HTML和CSS吗? – 2012-07-23 10:50:55

回答

1

检查是否item_bar被动画的时候,不要再排队行为,如果它是:

$('.item_bar_action_holder_drop a').click(function() { 
    var item_bar = $('.item_bar_action_holder_actions'); 
    if (item_bar.is(':animated')) { 
     return; 
    } 
    ... 
}); 
+0

对不起,它不工作。它的裤子像这样 – 2012-07-23 10:28:45

+0

@JohnMark也许你可以提供一个演示来展示你正在经历的确切行为。 http://jsfiddle.net/ – Petah 2012-07-23 10:31:06

0

Basic slideToggle() example.

我猜你的活动类的CSS是=显示:块;你的item_bar的默认CSS是display:none;如果我是正确的;你可以用这种方式太:

$(document).ready(function() { 
    $('.item_bar_action_holder_drop a').click(function() { 
     var item_bar = $('.item_bar_action_holder_actions'); 

     item_bar.removeClass('active');//removes class from all 
     $(this).addClass('active');//adds class clicked one 

     if(item_bar.is(':visible')) {//edited here ! 
     item_bar.slideToggle('fast',function() { 
      $(this).removeClass('active');//removes class when animate complete 
     }); 
     } 
    }); 
    //if you want to show first panel when your document is ready 
    $('.item_bar_action_holder_actions:first').show().addClass('active'); 
}); 

注意:您没有显示你的HTML,所以我试图猜测您的HTML。

+0

它是一个好主意,但是这个代码不能正常工作。它会永远不会打开,因为面板开始关闭 – 2012-07-23 10:34:29

+0

我更新了我的答案,你可以再次检查出来请 – 2012-07-23 10:44:16

+0

对不起,我发现问题,新代码是: – 2012-07-23 10:52:45