我也可以通过单击其他元素来展开/折叠 JQuery ui Accordion 的内容吗?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4681320/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Can I expand/collapse content of JQuery ui Accordion by click another elements too?
提问by qinHaiXiang
By default, there are headers of contents to control expand/collapse.But in my situation,I could expand/collapse the contents by another elements ,too. For example:
默认情况下,有内容标题来控制展开/折叠。但在我的情况下,我也可以通过其他元素展开/折叠内容。例如:
the basic structure of jquery ui accodion code:
jquery ui 代码的基本结构:
<script>
$(function() {
$( "#accordion" ).accordion();
});
</script>
<div class="demo">
<div id="accordion">
<h3><a href="#">Section 1</a></h3>
<div>
<p>
Mauris mauris ante, blandit et, ultrices a, suscipit eget, quam. Integer
ut neque. Vivamus nisi metus, molestie vel, gravida in, condimentum sit
amet, nunc. Nam a nibh. Donec suscipit eros. Nam mi. Proin viverra leo ut
odio. Curabitur malesuada. Vestibulum a velit eu ante scelerisque vulputate.
</p>
</div>
......
</div>
and now. I have another elements,just like :
现在。我有另一个元素,就像:
<ul id="another elements can expand/collapse too">
<li><a href=""> expand/collapse contents of section1 of id=accordion too</a></li>
........
</ul>
thank you very much!!
非常感谢您!!
采纳答案by justkt
You can use .activatewith 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
.
您可以使用.activate和传入的 false 以编程方式折叠所有元素。由于您一次只打开一个元素,因此这将折叠显示该链接的任何打开的元素。这仅在您collapsible
设置为true
.
You can pass in what other element you want to expand to expand that element on click.
您可以传入要扩展的其他元素以在单击时扩展该元素。
回答by Maksym Kozlenko
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);
}
});
回答by DrafFter
After update of JQuery UI there is no "active" method on accordion. So, to collapse all accordions use:
更新 JQuery UI 后,手风琴上没有“活动”方法。因此,要折叠所有手风琴,请使用:
$('.accordion').accordion('option', {active: false});
回答by sdailey
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.
感觉像一个黑客,但它是有效的,最好的。
回答by JPRLCol
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);