如何切换这个jsfiddle

时间:2014-10-13 17:29:57

标签: jquery slidetoggle toggleclass

我试图进行此切换,所以如果你点击一个,另一个关闭等

有什么想法吗?这是针对动态页面的,因此我无法为其添加唯一ID。

http://jsfiddle.net/lecollective/rhowLf9x/

$(document).ready(function() {  
 $(".cat-lead").click(function(e) {

   $(this).next(".product-list").slideToggle(500, 'linear'),
   $(this).toggleClass('droppedlist');
    e.preventDefault();

   });
});

var isActive = $(this).hasClass('droppedlist');
$('.product-list').removeClass('droppedlist'); 

$(this).stop(true,true);
if(!isActive){
    $(this).addClass("droppedlist");
}

任何帮助都会很棒!

1 个答案:

答案 0 :(得分:2)

在每次点击事件开始时,您可以重置所有不属于当前元素的事件:

var $allCats = $(".cat-lead");
$allCats.click(function(e) {
    //[Step 1] - Reset Others
    $allCats.not(this) //Exclude Clicked one
            .removeClass('droppedlist') //Remove *active* class (if exists)
            .next(".product-list") //Get All Lists
            .slideUp(500, 'linear'); //If already up, no effect shown

    //[Step 2] - Modify Current
    $(this).next(".product-list").slideToggle(500, 'linear'),
    $(this).toggleClass('droppedlist');
    e.preventDefault();
});

更新小提琴:http://jsfiddle.net/rhowLf9x/4/