簡體   English   中英

如果具有錨鏈接,如何使手風琴面板打開?

[英]How to make accordion panel open if it has anchor link?

我有手風琴。 我的想法是,如果手風琴內部有一個錨點,那么如果與錨點有鏈接,則相關的手風琴面板將打開。

我基本上要問的是這個。

  <script>
  $(function() {
    $( "#accordion" ).accordion();
  });
  </script>


<div id="accordion">
  <h3>Section 1</h3><a name="myAnchor"></a>
  <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>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>

如果要基於url打開該錨,則該錨的href需要使用相同的哈希(如果要使用該哈希)。

var hash = window.location.hash;
var hashName = hash && hash.replace('#','');
$('.accordionModule .accordionPanel .content a').filter(function(){
    return this.hash === hash;
    // or
     return this.name === hashName;       
}).closest('.accordionPanel').find('.trigger').click();

要么

$('.accordionModule .accordionPanel .content a[href$=' + hash +']')
  .closest('.accordionPanel').find('.trigger').click();

因此,url = mywebsite.com#myanchor將匹配<a href="#myanchor"></a>

例如,如果我正確了解所需功能:

var userUrl = window.location.href.split( '#' )[1];
$('.accordionModule .accordionPanel .content a').each(function( index ) {
    if ($( this ).attr('name') == userUrl ) {
        var $theTarget = $("a[href='" + userUrl + "']"); //gets the anchor targeted
        //then open the corresponding panel
        $theTarget.closest('.accordionPanel').find('.trigger').click();
    }
});

希望這就是您的要求。 已經晚了,我累了。 大聲笑

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM