卷起嵌套 jQuery Accordion 中的子元素

发布于 2024-09-14 09:34:20 字数 660 浏览 6 评论 0原文

当使用 jQuery 的 Accordion(以嵌套方式)时,我想确保当单击/打开父元素时,所有打开的子元素都会被关闭/卷起。我不确定尝试执行此操作时应该使用什么选择器。到目前为止,我已经尝试将“activate”设置为 false 来操纵更改事件,但这只会使任何打开的元素自动关闭。

假设我只有 1 个嵌套手风琴,我的 jquery 初始化如下所示:

$(".accordion").accordion({
            active: false, collapsible: true, autoHeight: false, animated: 'swing'
        });

        $(".child-accordion").accordion({
            active: false, collapsible: true, autoHeight: false, animated: 'swing',
            change: function(event, ui) { $(".child-accordion").accordion("activate", false); }
        });

其中 .child-accordion 是嵌套实例。当 .accordion 的成员打开时,我需要关闭 .child-accordion 下的任何内容。

When using jQuery's Accordion (in a nested fashion), I want to ensure that when a parent element is clicked/opened, any open children are closed/rolled up. I'm not sure what selector(s) I should use when attempting to do this. So far I've tried rigging a change event with "activate" set to false, but that simply makes any element that is opened automatically close.

Assuming I only have 1 nested accordion, my jquery initialization looks like:

$(".accordion").accordion({
            active: false, collapsible: true, autoHeight: false, animated: 'swing'
        });

        $(".child-accordion").accordion({
            active: false, collapsible: true, autoHeight: false, animated: 'swing',
            change: function(event, ui) { $(".child-accordion").accordion("activate", false); }
        });

where .child-accordion is the nested instance. I need anything under the .child-accordion to be closed when a member of the .accordion is opened.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

暖风昔人 2024-09-21 09:34:20
$(".accordion").accordion({
    collapsible: true,
    autoHeight: false,
    animated: 'swing',
    changestart: function(event, ui) {
        child.accordion("activate", false);
    }
});

var child = $(".child-accordion").accordion({
    active:false,
    collapsible: true,
    autoHeight: false,
    animated: 'swing'
});

您的版本无法正常工作的原因有两个

  1. 您的更改事件需要在父级上,因为那时您希望子级滚动

  2. 您需要创建事件 changestart 因为当您将 activate 设置为 false 时,它​​所做的主要事情是 切换子级中当前“可见”的部分,但是当父级上触发 change 事件时,子级已经隐藏,因此它不会执行任何操作。

编辑:这是此 http://jsfiddle.net/ryleyb/YPpEn 的工作版本/

$(".accordion").accordion({
    collapsible: true,
    autoHeight: false,
    animated: 'swing',
    changestart: function(event, ui) {
        child.accordion("activate", false);
    }
});

var child = $(".child-accordion").accordion({
    active:false,
    collapsible: true,
    autoHeight: false,
    animated: 'swing'
});

The reason your version wasn't working is two-fold

  1. Your change event needs to be on the parent, because that's when you want the children to roll up

  2. You need to make the event changestart because when you set activate to false, the main thing it does is toggle the currently "visible" section in the child, but when the change event triggers on the parent, the child is already hidden, so it doesn't do anything.

EDIT: here's a working version of this http://jsfiddle.net/ryleyb/YPpEn/

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文