重置 jQuery Accordion

发布于 2024-07-29 11:08:53 字数 1101 浏览 4 评论 0原文

我在一页上有两个手风琴。 我希望这样当单击/激活一个手风琴时,另一个手风琴(如果打开)将返回到其默认的折叠状态。

我尝试手动重新创建该功能,但它的问题太多,最终不允许我扩展手风琴。

我的代码:

$('#chicago-accordion').accordion({ autoHeight: false, collapsible: true, active:false, });
    $('#chicago-accordion h2').click(function () { 
                $('#ny-accordion .accordion-content').slideUp(); 
                $('#ny-accordion h2').removeClass("ui-state-active"); 
                $('#ny-accordion .accordion-content').removeClass("ui-accordion-content-active"); 
                $('#ny-accordion').accordion({ clearStyle:true, });

         });


$('#ny-accordion').accordion({ autoHeight: false, collapsible: true, active:false, });
    $('#ny-accordion h2').click(function () { 
                $('#chicago-accordion .accordion-content').slideUp();
                $('#chicago-accordion .accordion-content').removeClass("ui-accordion-content-active"); 
                $('#chicago-accordion h2').removeClass("ui-state-active"); 
                $('#chicago-accordion').accordion({ clearStyle:true, });
         });
            });

I have two accordions on one page. I would like it so that when one is clicked/activated the other accordion (if opened) will return to its default folded state.

I have tried to manually recreate the functionality but it is too buggy and eventually doesn't let me expand accordions.

My code:

$('#chicago-accordion').accordion({ autoHeight: false, collapsible: true, active:false, });
    $('#chicago-accordion h2').click(function () { 
                $('#ny-accordion .accordion-content').slideUp(); 
                $('#ny-accordion h2').removeClass("ui-state-active"); 
                $('#ny-accordion .accordion-content').removeClass("ui-accordion-content-active"); 
                $('#ny-accordion').accordion({ clearStyle:true, });

         });


$('#ny-accordion').accordion({ autoHeight: false, collapsible: true, active:false, });
    $('#ny-accordion h2').click(function () { 
                $('#chicago-accordion .accordion-content').slideUp();
                $('#chicago-accordion .accordion-content').removeClass("ui-accordion-content-active"); 
                $('#chicago-accordion h2').removeClass("ui-state-active"); 
                $('#chicago-accordion').accordion({ clearStyle:true, });
         });
            });

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

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

发布评论

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

评论(3

蓝色星空 2024-08-05 11:08:53

我找到的解决该问题的简单方法是销毁并重新创建手风琴。

var options = {autoHeight: false, collapsible: true, active:false};

$('#ac1').accordion(options);
$('#ac2').accordion(options);

$('#ac1').click(function () {
    $('#ac2').accordion('destroy').accordion(options);
});

$('#ac2').click(function () {
    $('#ac1').accordion('destroy').accordion(options);
});

The simplies solution I have found for that problem is to destroy and recreate the accordion.

var options = {autoHeight: false, collapsible: true, active:false};

$('#ac1').accordion(options);
$('#ac2').accordion(options);

$('#ac1').click(function () {
    $('#ac2').accordion('destroy').accordion(options);
});

$('#ac2').click(function () {
    $('#ac1').accordion('destroy').accordion(options);
});
别念他 2024-08-05 11:08:53

这是我用来重置单个手风琴的方法:

$('.ui-state-active.ui-accordion-header').trigger('click');

要重置特定的,您可以使用

$('.ui-state-active.ui-accordion-header.myclass1').trigger('点击');

$('.ui-state-active.ui-accordion-header.myclass2').trigger('点击');

Here's what I use to reset an single accordion:

$('.ui-state-active.ui-accordion-header').trigger('click');

To reset specific ones, you can use

$('.ui-state-active.ui-accordion-header.myclass1').trigger('click');

$('.ui-state-active.ui-accordion-header.myclass2').trigger('click');

把时间冻结 2024-08-05 11:08:53

这将重置您的手风琴,且没有选定的选项卡。
通过 addClass/removeClass/attr 进行重置将使您的手风琴在视觉上看起来不错,但需要单击 2 次才能激活先前选择的选项卡。

$('.yourAccordion').accordion({
    active: false
});

谢谢

This will reset your accordion with no selected tab.
Resetting through addClass/removeClass/attr will make your accordion looks good visually but it requires 2 clicks to activate previous selected tab.

$('.yourAccordion').accordion({
    active: false
});

Thanks

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