简体   繁体   English

打开特定的手风琴菜单

[英]Open specific Accordion menu

I'd like to visit my site: http://testsite.com/#accordion2 and for it to anchor down & open the 2nd accordion. 我想访问我的网站: http : //testsite.com/#accordion2 ,以便它锚定并打开第二个手风琴。 How do I achieve this? 我该如何实现? I'd also like this to happen for the first accordion if the url was #accordion1 . 如果网址是#accordion1我也希望第一次手风琴发生这种情况。

Here's my Fiddle: http://jsfiddle.net/jmjmotb3/ 这是我的小提琴: http : //jsfiddle.net/jmjmotb3/

 function close_accordion_section(source) { $(source).parent().find('.accordion-section-title').removeClass('active'); $(source).parent().find('.accordion-section-content').slideUp(300).removeClass('open'); } $('.accordion-section-title').click(function(e) { if($(e.target).is('.active')) { close_accordion_section(e.target); }else { $(this).addClass('active'); $(e.target).parent().find('.accordion-section-content').slideDown(300).addClass('open') } e.preventDefault(); }); 
 .accordion { overflow: hidden; margin-bottom: 40px; } .accordion-section { padding: 15px; border: 1px solid #d8d8d8; background: #fbfbfb; } .accordion-section-title { width: 100%; display: inline-block; background: url("http://placehold.it/50x50") top right no-repeat; } .accordion-section-title.active, .accordion-section-title:hover { text-decoration: none; } .accordion-section-content { padding: 15px 0; display: none; } 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script> <div id="accordion1" class="accordion"> <div class="accordion-section"> <a class="accordion-section-title" href="#accordion-1">More information</a> <div id="accordion-1" class="accordion-section-content"> <p>Text.</p> <p> </div> </div> </div> <div id="accordion2" class="accordion"> <div class="accordion-section"> <a class="accordion-section-title" href="#accordion-1">More information 2</a> <div id="accordion-1" class="accordion-section-content"> <p>Text.</p> <p> </div> </div> </div> 

Check the window.location.hash property and go from there. 检查window.location.hash属性,然后从那里开始。

document.addEventListener('DOMContentLoaded', function() {

    if (window.location.hash === 'x') {
        // do x
    }

});

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

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