Can I expand/collapse content of JQuery ui Accordion by click another elements too?

和自甴很熟 提交于 2019-12-01 16:11:32

You can use .activate with false passed in to collapse all elements programmatically. Since you only ever have one element open at a time, this will work to collapse whatever element is open showing that link. This will only work if you have collapsible set to true.

You can pass in what other element you want to expand to expand that element on click.

Collapse accordion tab:

$('.accordion').accordion('activate', false );

Expand first accordion tab:

$('.accordion').each(function (idx, item) {
    if ($(item).accordion("option", "active") === false) {
        $(item).accordion('activate', 0);
    }
});

After update of JQuery UI there is no "active" method on accordion. So, to collapse all accordions use:

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

I to had trouble getting the Accordions to collapse/expand after they were initially loaded. To get around this I used:

$('#accordionId h3').click();

...which mimics clicking the header area and will force toggle the activation classes.

Felt like a hack, but it's what worked, Best.

Using Bootstrap:

    //To Expand
    var elem  = $(this).find('.accordionElement');
    elem.addClass("in");
    elem.attr("style","");
    elem.attr("aria-expanded",true);

then you could collapse using:

    var elem  = $(this).find('.accordionElement');
    elem.removeClass("in");
    elem.attr("style","height: 0px;");
    elem.attr("aria-expanded",false);
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!