简体   繁体   English

通过外部链接打开手风琴面板

[英]Open an accordion panel from an external link

I have the following code and am trying to open an accordion panel from an external link. 我有以下代码,并尝试从外部链接打开手风琴面板。 I got the code as an example from a website and it has worked fine up until now however I can't seem to figure out how to do this. 我从一个网站上获得了代码作为示例,到目前为止,它一直运行良好,但是我似乎还不知道该怎么做。

jQuery: jQuery的:

jQuery(document).ready(function() {
    function close_accordion_section() {
        jQuery('.accordion .accordion-section-title').removeClass('active');
        jQuery('.accordion .accordion-section content').slideUp(300).removeClass('open');
    }

    jQuery('.accordion-section-title').click(function(e) {
        // Grab current anchor value
        var currentAttrValue = jQuery(this).attr('href');

        if(jQuery(e.target).is('.active')) {
            close_accordion_section();
        }else {
            close_accordion_section();

            // Add active class to section title
            jQuery(this).addClass('active');
            // Open up the hidden content panel
            jQuery('.accordion ' + currentAttrValue).slideDown(300).addClass('open'); 
        }

        e.preventDefault();
    });
});

HTML: HTML:

    <div class="accordion">
        <div class="accordion-section">
            <a class="accordion-section-title" href="#section-1">Section 1: </a>
            <div id="section-1" class="accordion-section-content">
                <p>Content goes here</p>
            </div><!--end .accordion-section-content-->
        </div><!--end .accordion-section-->

        <div class="accordion-section">
            <a class="accordion-section-title" href="#section-2">Section 2: </a>
            <div id="section-2" class="accordion-section-content">
                <p>Content goes here</p>
            </div><!--end .accordion-section-content-->
        </div><!--end .accordion-section-->

So far I have tried the external link as: 到目前为止,我已经尝试将外部链接设置为:

<a href="#section-2.active">Link</a>

... and a few things similar to that but no joy yet unfortunately. ...和一些与此类似的东西,但不幸的是没有喜悦。 I've also fiddled around with the jQuery but also didn't get anywhere. 我也玩弄了jQuery,但是却一无所获。

I've not had much experience with jQuery and I'm trying to learn it as I go so please be thorough with any answers so I can learn. 我没有jQuery的丰富经验,因此我正在尝试学习它,因此请彻底回答所有问题以便我学习。 Thanks very much! 非常感谢!

When the page loads your ready function will run, this should open the accordion section the URI's hash refers to: 页面加载后,您的ready函数将运行,这将打开URI 哈希所指的手风琴部分:

jQuery(document).ready(function() {
    // get the #section from the URL
    var hash = window.location.hash; 
    // open accordion
    jQuery(hash).slideDown(300).addClass('open'); 
    // set title to active 
    jQuery(hash).prev('.accordion-section-title').addClass('active');
    /* ... the rest of your code here */
});

you might get a slight performance boost if you don't get the jQuery object twice, you can chain these like this: 如果您没有两次获得jQuery对象,则可能会稍微提高性能,您可以像这样链接这些对象:

    jQuery(hash).slideDown(300).addClass('open')
                .prev('.accordion-section-title').addClass('active');

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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