简体   繁体   English

此jQuery slideUp代码使用的正确选择器是什么?

[英]What's the correct selector to use for this jQuery slideUp code?

I am using Foundation to create a page where I will have multiple collapsed buttons. 我正在使用Foundation创建一个页面,其中有多个折叠按钮。 When you click on the button, the hidden content within it will slide down and if there are any expanded buttons they will slide up. 当您单击该按钮时,其中的隐藏内容将向下滑动,如果有任何展开的按钮,它们将向上滑动。

I have this partially working, but because I have the buttons in pairs of two I can't figure out the right selector to get the expanded divs to close regardless of where they're positioned. 我有部分工作,但是因为我有两个成对的按钮,所以我想不出正确的选择器来关闭展开的div,无论它们位于何处。

Here is my HTML: 这是我的HTML:

<div class="large-6 columns">
    <div class="box">
        Initial Content
        <div class="hidden">
            This is hidden content
        </div>
    </div>
    <div class="box">
        Initial Content
        <div class="hidden">
            This is hidden content
        </div>
    </div>
</div>

<div class="large-6 columns">
    <div class="box">
        Initial Content
        <div class="hidden">
            This is hidden content
        </div>
    </div>
    <div class="box">
        Initial Content
        <div class="hidden">
            This is hidden content
        </div>
    </div>
</div>

CSS: CSS:

.box {
    height:auto;
    background:#000;
    color:#fff;
    cursor:pointer;
    width:200px;
    margin-bottom:1em;
}
.hidden {
    height:200px;
    display:none;
}
    .hidden.open {
        display:block;
    }

And Javascript: 和Javascript:

$(document).ready(function() {
    $('.box').click(function() {
        $(this).find('.hidden').slideToggle();
        $(this).siblings().find('.hidden').slideUp();
        return false;
    });
});

Here is a JSFiddle to demonstrate what I have so far. 这是一个JSFiddle,以演示到目前为止的内容。

You could add one line to take care of it: 您可以添加一行来处理它:

$(this).parent().siblings().find('.hidden').slideUp();

jsFiddle example jsFiddle示例

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM