admin管理员组文章数量:1382622
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
}
});
JSFiddle Link
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 bee closed. I'm not sure how I can achieve this. Are you able to help?
Thanks
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
}
});
JSFiddle Link
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 bee closed. I'm not sure how I can achieve this. Are you able to help?
Thanks
Share Improve this question edited Jul 1, 2011 at 9:03 Sivakanesh asked Jul 1, 2011 at 8:17 SivakaneshSivakanesh 8374 gold badges13 silver badges38 bronze badges 5-
#2 is the default behavior when you use
collapisble: true
– prodigitalson Commented Jul 1, 2011 at 8:21 -
When
header1
is open and you clickheader2
, thenheader1
is closed andheader2
is opened. However what I would like is whenheader1
is opened and you clicked onheader1
again, thenheader1
to be closed. That does not happen at the moment. – Sivakanesh Commented Jul 1, 2011 at 8:26 -
1
yes if you have collapisble set it should work that way: jqueryui./demos/accordion/#collapsible Its not working now because youve mispelled the option its
collapsible
NOTcollapsable
– prodigitalson Commented Jul 1, 2011 at 8:41 - Sivakanesh, do you know that ID's are case sensitive and that they cannot start with a 'number' ? use rather something like id="link1" id="link2" ...etc. – Roko C. Buljan Commented Jul 1, 2011 at 9:02
- Updated the questions and put up a JSFiddle link. Tx – Sivakanesh Commented Jul 1, 2011 at 9:08
1 Answer
Reset to default 7When the accordion changes, a jQuery object wrapping the active header's <h3>
element is passed in ui.newHeader
, so you only have to use find():
var currentHeaderID = ui.newHeader.find("a").attr("id");
Updated fiddle here.
本文标签: javascriptGet reference to a JQuery UI Accordion HeaderStack Overflow
版权声明:本文标题:javascript - Get reference to a JQuery UI Accordion Header - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1743986776a2571411.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论