从同一页面和外部页面上的锚点链接打开Accordion Panel

时间:2013-10-17 09:08:53

标签: javascript html

我有一个Accordion按照标记,CSS和jquery与给定相同。我无法建立外部锚点链接(同一页面和其他页面)来触发打开特定面板。手风琴来自模板。

HTML

<!-- Accordions -->
<h1 class="h-margin">Accordions</h1>
<div class="accordion-trigger">Categories</div>     
<div class="accordion-container" data-panel="0">
    Pellentesque habitant 
</div>     

<div class="accordion-trigger">Categories</div> 
<div class="accordion-container" data-panel="1">
    Pellentesque habitant morbi 
</div>

<div class="accordion-trigger">Categories</div>         
<div class="accordion-container" data-panel="2">
    Pellentesque habitant morbi tris
</div>
<!-- ENDS Accordions -->

<a href="#">Open Section 1</a>

<a href="#">Open Section 2</a>

<a href="#">Open Section 3</a>

JavaScript

// Accordion box
$('.accordion-container').hide(); 
$('.accordion-trigger:first').addClass('active').next().show();
$('.accordion-trigger').click(function(){
    if( $(this).next().is(':hidden') ) { 
        $('.accordion-trigger').removeClass('active').next().slideUp();
        $(this).toggleClass('active').next().slideDown();
    }
    return false;
});

1 个答案:

答案 0 :(得分:0)

它会帮助你:

 // open content that matches the hash
 var hash = window.location.hash;
 var thash = hash.substring(hash.lastIndexOf('#'), hash.length);
 $('.accordion').find('a[href*='+ thash + ']').closest('h3').trigger('click');