【发布时间】:2016-01-22 19:24:19
【问题描述】:
这个很棘手!我在这上面花了好几个小时,在 Stackoverflow 上找不到类似的东西,可能是因为我不确定要搜索什么。
问题:
在一个容器中,我有 3 个盒子,每个盒子都有一个打开/关闭切换按钮 - 可以单独切换它们 - 效果很好。
我在容器外有一个 Open-Close All 按钮,它应该能够打开剩余的框
(if 1 or 2 are already open)或者如果所有/或没有打开,它应该打开/关闭它们。
我的代码几乎可以完成我需要的所有工作(if 1 or 2 boxes are open and you click Open-Close All, the remainder opens),如果所有框都已关闭,则打开-关闭会立即将它们全部打开。
唯一不起作用的是,如果所有框都打开了,我希望能够通过单击打开-关闭所有来一次关闭它们。
http://codepen.io/StrengthandFreedom/pen/ZbrvOO
$('.small-box-button').on('click', function(){
event.preventDefault();
$(this).next('.small-box').toggleClass('is-visible');
});
// Open / Close all / remainders
$('.open-close-all-button').on('click', function(){
event.preventDefault();
if ($('.small-box').is(':visible')) {
// then open the small boxes that are not open yet (the remainders)
$('.small-box').siblings().addClass('is-visible');
// $(this).next('.small-box').toggleClass('is-visible');
}
//not sure what to do here...
else ($('.small-box').not(':visible'))
$('.small-box').siblings().addClass('is-visible');
});
我认为我需要使用更多 if else 条件并检查值 (like if isVisible >= 1 || 2 ) 但不知道如何编写它。
我有一种感觉,这可以写得更简单。
非常感谢一些指导,我已尽力使示例尽可能易于查看。
谢谢! :-)
【问题讨论】:
-
是的,应该是可行的......
-
您的代码笔上没有“打开/关闭”按钮?
标签: javascript jquery html css