使用jQuery仅扩展一个部分 [英] Expand only one section w/ jQuery

查看:58
本文介绍了使用jQuery仅扩展一个部分的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道它已经完成了,但是我想自己从头开始写这个……

I know it's been done before but I'm trying to write this from scratch myself...

我有Q&具有以下格式的部分:

I have Q & A section in the following format:

<div class="Q">aaaa</div>
<div class="A">bbbb</div>

<div class="Q">cccc</div>
<div class="A">dddd</div>

我需要单击Q并在下面立即展开A.一次只能扩展一个部分.例如,如果我将A:bbbb展开,然后单击Q:cccc,则需要折叠A:bbbb并展开A:dddd.

I need to click Q and expand A immediately below. Only one section should be expanded at a time. For example, if I have A:bbbb expanded and then I click Q:cccc I need to collapse A:bbbb and expand A:dddd.

这是到目前为止我得到的:

Here's what I've got so far:

$(".A").hide();
$(".Q").click(function() {
    $(this).next("div").slideToggle("slow");
});

如何在打开新部分之前折叠任何可能打开的部分,而不先使其在扩展之前折叠?

How do I collapse any possibly open section before opening the new one without making it collapse first before expanding?

推荐答案

像这样简单:

$('.q').click(function() {
    $('.a:visible').slideUp('slow');
    $(this).next('.a').is(":hidden").slideDown('slow');
})

这篇关于使用jQuery仅扩展一个部分的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆