2012-12-22 32 views
2

我有一个相当简单的jQuery切换在这里工作:http://jsfiddle.net/yVa3U/1/ - 但我不知道如何获得活动div切换下一个div被点击时切换。jQuery在多个div之间打开/关闭

有关如何做到这一点的任何建议?

感谢

$(document).ready(function() { 

    $('.answer').hide(); 

    $(".toggle").click(function(){ 

     $("div[rel='profile_" + $(this).attr("profile") + "']").toggle(400); 
}); 

});​ 

回答

4

您应该添加

$(".answer").not($(this).next()).hide(400); 

调用toggle()功能

旁注

  1. 之前,你可以行$("div[rel='profile_" + $(this).attr("profile") + "']").toggle(400);改变 $(this).next().toggle(400);
  2. 而不是使用属性profile你最好使用data属性

Demo

1

您需要hideall答案上点击exceptcurrent答案,你能做到这样,

Live Demo

$(document).ready(function() { 
    $('.answer').hide(); 
    $(".toggle").click(function() { 
     currentAnswerDiv = $("div[rel='profile_" + $(this).attr("profile") + "']"); 
     $('.answer').not(currentAnswerDiv).hide(); 
     currentAnswerDiv.toggle(400); 
    }); 
});​ 
+0

如果同时点击两次相同的切换元素,则不会切换活动答案 – charlietfl

+0

@charlietfl,我已更新我的答案,谢谢指出。 – Adil