简体   繁体   中英

Open Custom Accordion from Link

I know there are many posts on this subject, but I have created a custom accordion due to my HTML markup. Below is my JQuery.

$(function(){
  hide(0);
  $('.accordion > h1').click(function(){
    hide($('.accordion').index($(this).parent()));
  });
  function hide(i){
    $('.accordion').removeClass('active');
    $('.accordion:eq(' + i + ')').addClass('active');
  }
});

Here is my HTML for the Accordion. Note I use ExpressionEngine CMS.

<section class="module flag-{style} {page_module_style}">
  <h1 id="row-id{row_id}">{header}</h1>
  <div class="copy">
    {copy}
  </div>
</section>

I use CSS to give it the Accordion look. the link will be http://siteurl.com/page#row-id1234 , I need to figure out how to make it to where when I link to the specific accordion tab with its ID that it will open and focus on that tab.

When the window is ready get the current hash and find the element that matches that id:

function getHash() {
  var hash = window.location.hash;
  return hash.substring(1); // remove #
}

$(function(){
  activeTab = $(getHash()).parent()

  hide($('.accordion').index(activeTab));
});

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM