jquery手风琴,打开一个基于href的盒子

And*_*ndy 9 jquery accordion

我正在尝试根据我发送到页面的链接打开手风琴

这是我的网址

services.html#品牌

我正在使用以下代码:

 <script type="text/javascript">
      $(document).ready(function(){
     $('#accordion').accordion({collapsible: true, animated: 'slide', autoHeight: false, navigation: true, active : 'false'});
      });
  </script>
Run Code Online (Sandbox Code Playgroud)

手风琴看起来像:

<div id="accordion">
<h3 id="branding"><a href="#">Branding</a></h3>
<div>
<p>Does your business have a</p>
</div>
<h3><a href="#print">Print</a></h3>
<div>
<p>Brochures</a></p>
</div>
</div>
Run Code Online (Sandbox Code Playgroud)

任何帮助将不胜感激... http://docs.jquery.com/UI/Accordion

Mot*_*tie 14

哦,我看到Jeff报告说当前的UI版本已经坏了,但我实际上有一个使用当前版本的解决方案......

HTML

<div id="accordion">
 <h3><a href="#branding">Branding</a></h3>
 <div>
  <p>Does your business have a</p>
 </div>
 <h3><a href="#print">Print</a></h3>
  <div>
  <p>Brochures</p>
  </div>
</div>
Run Code Online (Sandbox Code Playgroud)

脚本

$(function(){
 $('#accordion').accordion({
  collapsible: true,
  animated: 'slide',
  autoHeight: false,
  navigation: true
 });
 // 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');
})
Run Code Online (Sandbox Code Playgroud)

a[href$=...]最初使用过,但是把它改成了a[href*=...]...要么会起作用


更新:该navigation选项已从jQuery UI 1.10.0中删除,因此请使用此方法:

CSS

.accordion {
  position: relative;
}
.accordion .accordion-link {
  position: absolute;
  right: 1%;
  margin-top: 16px;
  z-index: 1;
  width: 12px;
  height: 12px;
  background: url(link.png) center center no-repeat; /* approx 12x12 link icon */
}
Run Code Online (Sandbox Code Playgroud)

脚本

var hashId = 0,
  $accordion = $('#accordion');
if (window.location.hash) {
  $accordion.children('h3').each(function(i){
    var txt = $(this).text().toLowerCase().replace(/\s+/g,'_');
    this.id = txt;
    if (txt === window.location.hash.slice(1)) {
      hashId = i;
    }
  });
}

$accordion.accordion({
  active: hashId,
  animate: false,
  heightStyle: 'content',
  collapsible: true,
  create: function( event, ui ) {
    $accordion.children('h3').each(function(i){
      $(this).before('<a class="accordion-link link" data-index="' + i + '" href="#' + this.id + '"></a>');
    });
    $accordion.find('.accordion-link').click(function(){
      $accordion.accordion( "option", "active", $(this).data('index') );
    });
  }
});
Run Code Online (Sandbox Code Playgroud)