jquery accordion, opening a box based on href

放肆的年华 提交于 2019-12-30 03:14:27

问题


I am trying to open an accordion based on a link i send to the page

This is my url

services.html#branding

I am using the following code in the head:

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

And the accordion looks like:

<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>

Any help would be greatly appreciated... http://docs.jquery.com/UI/Accordion


回答1:


Oh I see Jeff reported that the current UI version is broken, but I actually had a solution using the current version...

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>

Script

$(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');
})

I used a[href$=...] originally, but changed it to a[href*=...]... either will work


Update: the navigation option was removed from jQuery UI 1.10.0, so use this method:

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 */
}

Script

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') );
    });
  }
});



回答2:


The bad news is that the accordion plugin is currently broken (as of 1.7.2, which you can see from ticket #4653). The good news is that it's fixed, and you can already grab the latest version here - but beware, it isn't a stable release yet!

If you use the 1.8.1 code, the navigation feature works again. Setting navigation: true will direct accordion to automatically open the correct panel when you browse to a url that matches your navigation fragment (so that your example would work: services.html#branding).

I think you also want to add the missing identifier to your branding anchor tag, like this:

<h3 id="branding"><a href="#branding">Branding</a></h3>

Finally, you may want to use the technique described here to update your page's url to reflect which accordion panel has been clicked without reloading your page.




回答3:


Easiest way to do this is using focusin.

     $(function() {
    $( "#accordion" ).accordion({
        event: "focusin"
    });
});


<div id="accordion">
<h3 id="section-1">Section 1</h3>
<div>
    <p>blaa </p>
</div>
<h3 id="section-2">Section 2</h3>
<div>
    <p>bla</p>
</div>

you can href from the same page or from a different page simply by doing

<a href "otherpage.html#section-1">click to go to section 1</a>



回答4:


See: Activate Accordion Section By URL Hash

Demo: found here.

TL;DR … Here’s the code:

$( "#accordion" ).accordion({

    // Called when the widget instance is created.
    create: function( e, ui ) {

        // The accordion DOM element.
        var $this = $( this );

        // Triggered when the browser hash changes.
        $( window ).on( "hashchange", function( e ) {

                // The selector string used to find all accordion headers.
            var headers = $this.accordion( "option", "header" ),

                // The header we're looking for, based on the location hash.
                header = $( location.hash ),

                // The index of the header we're looking for.
                index = $this.find( headers ).index( header );

            // If there's a valid index, activate the accordion section.
            // Otherwise, do nothing.
            if ( index >= 0 ) {
                $this.accordion( "option", "active", index );    
            }

        });

        // Make sure this works on initial page load.
        $( window ).trigger( "hashchange" );

    }

});

… and the HTML:

<div id="accordion">
  <h3 id="section1">Section 1</h3>
  <div>
    <p>
    Mauris mauris ante, blandit et, ultrices a, suscipit eget, quam. Integer
    ut neque. Vivamus nisi metus, molestie vel, gravida in, condimentum sit
    amet, nunc. Nam a nibh. Donec suscipit eros. Nam mi. Proin viverra leo ut
    odio. Curabitur malesuada. Vestibulum a velit eu ante scelerisque vulputate.
    </p>
  </div>
  <h3 id="section2">Section 2</h3>
  <div>
    <p>
    Sed non urna. Donec et ante. Phasellus eu ligula. Vestibulum sit amet
    purus. Vivamus hendrerit, dolor at aliquet laoreet, mauris turpis porttitor
    velit, faucibus interdum tellus libero ac justo. Vivamus non quam. In
    suscipit faucibus urna.
    </p>
  </div>
  <h3 id="section3">Section 3</h3>
  <div>
    <p>
    Nam enim risus, molestie et, porta ac, aliquam ac, risus. Quisque lobortis.
    Phasellus pellentesque purus in massa. Aenean in pede. Phasellus ac libero
    ac tellus pellentesque semper. Sed ac felis. Sed commodo, magna quis
    lacinia ornare, quam ante aliquam nisi, eu iaculis leo purus venenatis dui.
    </p>
    <ul>
      <li>List item one</li>
      <li>List item two</li>
      <li>List item three</li>
    </ul>
  </div>
  <h3 id="section4">Section 4</h3>
  <div>
    <p>
    Cras dictum. Pellentesque habitant morbi tristique senectus et netus
    et malesuada fames ac turpis egestas. Vestibulum ante ipsum primis in
    faucibus orci luctus et ultrices posuere cubilia Curae; Aenean lacinia
    mauris vel est.
    </p>
    <p>
    Suspendisse eu nisl. Nullam ut libero. Integer dignissim consequat lectus.
    Class aptent taciti sociosqu ad litora torquent per conubia nostra, per
    inceptos himenaeos.
    </p>
  </div>
</div>

Worked well for my needs!




回答5:


Here's how to do it...

It will update the hash for you based on what is inside your h3 tags.

You can also specify an index by setting an attribute on your accordion container div like this... data-active-index="2"

var initAccordion = function(_t) {
    _t.each(function() {
        var obj = {
            heightStyle: 'content',
            collapsible: true,
            beforeActivate: function( event, ui ) {
                window.location.hash = ui.newHeader.attr('id');
            }
        };
        // preset the active tab in html [0 = first tab]
        var attr = $(this).attr('data-active-index');
        obj.active = null;
        if(attr !== null && attr !== undefined) {
            obj.active = Number(attr);
        }
        // human readable ids        
        var hash = window.location.hash;
        $(this).find('>h3').each(function(i){
            this.id = $(this).text().toLowerCase().replace(/[^a-z0-9]/g, function(s) {
                var c = s.charCodeAt(0);
                if (c == 32) return '_'; // space
                return '';
            });
            if(hash && hash == '#'+this.id) {
                obj.active = i;
            }
        });
        $(this).accordion(obj);
    });
};initAccordion($(".accordion"));


来源:https://stackoverflow.com/questions/1861030/jquery-accordion-opening-a-box-based-on-href

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!