How to link to specific accordion section from dif

2019-08-30 23:25发布

I have successfully built a JQuery accordion using Soh Tanaka's Simple Accordion tutorial. I would like to be able to link to a specific accordion section from another page but I'm not sure how to do this. Any help would be greatly appreciated as I haven't had much luck finding any help specific to this tutorial. Thanks!

HTML:

<h2 class="acc_trigger"><a href="#">Web Design &amp; Development</a></h2>
<div class="acc_container">
<div class="block">
<!--Content Goes Here-->
</div>
</div>

JQuery:

//Set default open/close settings
$('.acc_container').hide(); //Hide/close all containers
$('.acc_trigger:first').addClass('active').next().show(); //Add "active" class to first trigger, then show/open the immediate next container

//On Click
$('.acc_trigger').click(function(){
if( $(this).next().is(':hidden') ) { //If immediate next container is closed...
    $('.acc_trigger').removeClass('active').next().slideUp(); //Remove all "active" state and slide up the immediate next container
    $(this).toggleClass('active').next().slideDown(); //Add "active" state to clicked trigger and slide down the immediate next container
}
return false; //Prevent the browser jump to the link anchor
});

1条回答
对你真心纯属浪费
2楼-- · 2019-08-30 23:54

You should really use JQuery UI accordion. Then if what you want is to be able to select a certain accordion section by script you can use the following:

    $('#myAccordion').accordion("option", "active", 0)

With this code, once you have created an accordion element built on a tag with id "myAccordion", you select the first page (namely, indexed with 0). To select the second page you can pass 1 for the last parameter and so on.

查看更多
登录 后发表回答