我试图实现展开/折叠所有功能。 我有一个可扩展项目的列表,右上角有一个按钮,允许用户展开和折叠所有内容。折叠并展开全部
目前,如果所有内容一起展开/折叠,但我的代码在用户扩展一些(而不是全部)元素时无法正常工作。它只是切换折叠/展开的元素。 JSFiddle
这里是我的jQuery代码正在运行:
$('div.srcprojects').hide();
$('.projectscontainer').on("click", "span.destproject", function(){
$(this).siblings('.srcprojects').toggle().end().siblings('div.destarrow').toggleClass("arrow-right arrow-down");
}).on("click", "div.destarrow", function(){
$(this).parent().find("span.destproject").trigger("click");
});
$('body').on("click", "span#expandcollapse", function(){
$(this).text(function(i, currentText){
return currentText === 'Expand All' ? 'Collapse All' : 'Expand All';
});
$('body').find("span.destproject").trigger("click");
});
我怎样才能使它所以我#expandcollapse实际上扩展所有/折叠所有?
正如Steve在他的回答中指出的那样,每次拨动开关时,都会根据项目的当前状态从“隐藏”状态切换到“显示”状态。要达到所需效果,需要在折叠时在每个可用的可见元素上运行.hide(),在展开时运行.show()。 - 或使用切换(true)/切换(false)。 – BrianHall 2013-03-20 22:32:44