gpt4 book ai didi

javascript - 打开特定的 Accordion 菜单

转载 作者:太空宇宙 更新时间:2023-11-04 11:08:01 24 4
gpt4 key购买 nike

我想访问我的网站:http://testsite.com/#accordion2并让它锚定并打开第二个 Accordion 。我如何实现这一目标?如果 url 是 #accordion1,我也希望第一个 Accordion 发生这种情况。

这是我的 fiddle :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
}

});

关于javascript - 打开特定的 Accordion 菜单,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33813865/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com