获取对 JQuery UI Accordion 标头的引用
我有一个 JQuery 手风琴,如下所示;
<div id="accordion">
<h3 class="ui-accordion-header"><a id="link1" href="#">First Header</a></h3>
<div id="div1">First Content</div>
<h3 class="ui-accordion-header"><a id="link2" href="#">Second Header</a></h3>
<div id="div2">Second Content</div>
</div>
手风琴是这样生成的:
$("#accordion").accordion({
collapsible:true,
active:false,
navigation:true,
autoHeight:false,
change:function(event, ui){
var index = $(this).find("h3").index(ui.newHeader[0]);
var header = $(this).find("h3")[index].find("a"); //<--- problem line
var currentHeaderID = (header.attr("id")); //<--id that I need
}
});
手风琴加载正常。我正在努力实现两件事。
1- 获取刚刚打开的 header 标签内的 href 元素的 ID(即 ids link1 和 link2)。上面的更改事件中的代码为我提供了标头的索引。但我正在努力让下一行 (var header = ....
) 正常工作。您是否能够
2-已解决 当用户单击已打开的标题时,该部分将关闭,因此实际上所有部分都会关闭。我不知道如何才能实现这一目标。你能帮忙吗?
谢谢
I have a JQuery Accordion as below;
<div id="accordion">
<h3 class="ui-accordion-header"><a id="link1" href="#">First Header</a></h3>
<div id="div1">First Content</div>
<h3 class="ui-accordion-header"><a id="link2" href="#">Second Header</a></h3>
<div id="div2">Second Content</div>
</div>
The Accordion is generated by this:
$("#accordion").accordion({
collapsible:true,
active:false,
navigation:true,
autoHeight:false,
change:function(event, ui){
var index = $(this).find("h3").index(ui.newHeader[0]);
var header = $(this).find("h3")[index].find("a"); //<--- problem line
var currentHeaderID = (header.attr("id")); //<--id that I need
}
});
The accordion is loading up fine. I'm trying to achieve two things.
1- Get the ID of the href element inside the tag of the header that was just opened (i.e. ids link1 and link2). The code above inside the change event is giving me the index of the header. But I'm struggling to get the next line (var header = ....
) working. would you be able to
2- RESOLVED When a user clicks on an already opened header, that section is closed, so effectively all sections become closed. I'm not sure how I can achieve this. Are you able to help?
Thanks
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
当手风琴发生变化时,包装活动标头的
元素的 jQuery 对象会在
ui.newHeader
中传递,因此您只需使用 find():更新了小提琴 这里。
When the accordion changes, a jQuery object wrapping the active header's
<h3>
element is passed inui.newHeader
, so you only have to use find():Updated fiddle here.