link & 打开 Accordion-6ren"> link & 打开 Accordion-我通常从以下位置获取代码:https://www.w3schools.com .这个代码还有一个 Accordion ; 但是,当我使用链接 -> 到 Accordion 时, Accordion 不-6ren">
gpt4 book ai didi

javascript - 使用 link & 打开 Accordion

转载 作者:行者123 更新时间:2023-11-28 01:13:04 29 4
gpt4 key购买 nike

我通常从以下位置获取代码:https://www.w3schools.com .这个代码还有一个 Accordion ;

但是,当我使用链接 -> 到 Accordion 时, Accordion 不会跳开,而是保持关闭状态。如果有人链接了一个包含 Accordion 内容的链接,有人知道如何打开 Accordion 吗?

谢谢。

var acc = document.getElementsByClassName("accordion");
var i;

for (i = 0; i < acc.length; i++) {
acc[i].onclick = function() {
this.classList.toggle("active");
var panel = this.nextElementSibling;
if (panel.style.maxHeight){
panel.style.maxHeight = null;
} else {
panel.style.maxHeight = panel.scrollHeight + "px";
}
}
}
@charset "utf-8";
/* CSS Document */

/* Style the buttons that are used to open and close the accordion panel */
button.accordion {
background-color: #530010;
color: #FFF;
font-weight: bolder;
cursor: pointer;
padding: 10px;
width: 100%;
text-align: left;
border: none;
outline: none;
transition: 0.4s;
border-radius: 5px;
}

/* Add a background color to the button if it is clicked on (add the .active class with JS), and when you move the mouse over it (hover) */
button.accordion.active, button.accordion:hover {
background-color: #a20121;
}

/* Style the accordion panel. Note: hidden by default */
div.panel {
padding: 0px 5px;
margin-bottom: 5px;
background-color: #none;
max-height: 0px;
overflow: hidden;
transition: max-height 0.2s ease-out;
}

button.accordion:after {
content: '\02795'; /* Unicode character for "plus" sign (+) */
font-size: 13px;
color: #FFF;
font-weight: bolder;
float: right;
margin-left: 5px;
}

button.accordion.active:after {
content: "\2796"; /* Unicode character for "minus" sign (-) */
}
<a href="schedule.html#0906">2018.09.06</a>(木) 今池GROW<br><br>

<button class="accordion"><a name="0906">■</a>2018/09/06(木)今池GROW </button>
<div class="panel">
<p>
Testing
</p>
</div>

最佳答案

最初我有点难以理解您的问题,但我还是试了一下!

为 .accordion 按钮添加了 ID。为文本链接添加了 .accordion-link 类。当点击 Accordion 链接时,哈希被隔离,并用于触发具有相应 ID 的 Accordion 按钮上的点击事件。

fiddle :https://jsfiddle.net/ya6vtosc/25/

JS

var acc = document.getElementsByClassName("accordion");
var i;

for (i = 0; i < acc.length; i++) {
acc[i].onclick = function() {
this.classList.toggle("active");
var panel = this.nextElementSibling;
if (panel.style.maxHeight){
panel.style.maxHeight = null;
} else {
panel.style.maxHeight = panel.scrollHeight + "px";
}
}
}

// get list of links by class
var links = document.getElementsByClassName("accordion-link");

var linksLength = links.length;
for(var i=0; i < linksLength; i++){
links[i].onclick = function(e){
// preventDefault is probably optional
// depending on your application
e.preventDefault();

// isolate the hash
var hash = e.path[0].hash;

// remove # from hash
hash = hash.substring(1, hash.length);

// select by id using hash
document.getElementById(hash).click();
}

HTML

<a href="schedule.html#0906" class="accordion-link">2018.09.06</a>(木) 今池GROW<br><br>


<button class="accordion" id="0906">2018/09/06(木)今池GROW </button>
<div class="panel">
<p>
Testing
</p>
</div>

编辑 - 我还从您的按钮中删除了您的链接标签...不确定标签是否允许在按钮中使用或不在我的脑海中,但感觉不对所以我放弃了它们。

关于javascript - 使用 <a href ="#link.html">link</a> & <a name ="link"></a> 打开 Accordion ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52006937/

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