打开特定的Accordion菜单

时间:2015-11-19 20:34:24

标签: javascript jquery html css

我想访问我的网站: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>

1 个答案:

答案 0 :(得分:1)

检查window.location.hash属性并从那里开始。

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

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

});