简体   繁体   English

多个下拉窗口,打开另一个时会自动关闭

[英]Multiple drop down windows that auto-close when opening another

I have an example of what I'm working on at JSFiddle so you can get an idea of what I'm working on looks like. 我有一个关于我在JSFiddle工作的例子,所以你可以了解我正在做什么样的。 I'm trying to find a way to make the drop-down that's opened auto close when opening another. 我正试图找到一种方法,使打开另一个时打开的下拉菜单自动关闭。 All the solutions I find either keep anything from opening or opening & closing everything at the same time. 我找到的所有解决方案都可以保持打开或同时打开和关闭所有内容。 Keep in mind on the final page their will be around 30 of these drop-downs. 请记住最后一页,他们将在这些下拉菜单中约有30个。

Here is the jquery code as is. 这是jquery代码。

    $(document).ready(function () {
        $(".answer").hide();

        $(".question").click(function () {
            $(this).next(".answer").slideToggle(500);
        });
    });

The html is set up like this. html就是这样设置的。

<p class="question">The question.</p>
<div class="answer">The answer.</div>
<p class="question">The question2.</p>
<div class="answer">The answer2.</div>
<p class="question">The question3.</p>
<div class="answer">The answer3.</div>
<p class="question">The question4.</p>
<div class="answer">The answer4.</div>

I'm very new to jquery & I'm doing my best here. 我对jquery很新,我在这里做得最好。 Ive been working at this all day and only come asking for help as a last resort. 我一整天都在这里工作,只是来寻求帮助作为最后的手段。 If more info is needed Ill do my best to provide. 如果需要更多信息我会尽力提供。 Any help is greatly appreciated! 任何帮助是极大的赞赏!

Close all open answers only if the clicked answer isn't visible: http://jsfiddle.net/zFuUp/2/ 仅当单击的答案不可见时才关闭所有打开的答案: http//jsfiddle.net/zFuUp/2/

$(document).ready(function() {
    $(".answer").hide();
    //toggle the componenet with class msg_body
    $(".question").click(function() {
        // Check if we are closing us
        if( ! $(this).next('.answer').is(':visible') )
            $('.answer').slideUp(500)

        // Open us
        $(this).next(".answer").slideToggle(500);
    });
});

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

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