簡體   English   中英

打開特定的手風琴菜單

[英]Open specific Accordion menu

我想訪問我的網站: http : //testsite.com/#accordion2 ,以便它錨定並打開第二個手風琴。 我該如何實現? 如果網址是#accordion1我也希望第一次手風琴發生這種情況。

這是我的小提琴: 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> 

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