gpt4 book ai didi

javascript - 定位 Accordion 中的切换按钮

转载 作者:太空宇宙 更新时间:2023-11-04 09:14:27 25 4
gpt4 key购买 nike

我正在尝试将我的开/关切换控件作为目标,该控件嵌入在一个链接中,当您点击它时,该链接也会切换 Accordion 。两个开关都可以工作,但我希望它们彼此独立工作。开/关开关不应该切换 Accordion 。我正在使用 Ratchet 作为框架,如果这意味着什么的话。 Here's my JS Fiddle

<li class="table-view-cell toggle-handle accordion">
Kitchen Light
<div class="toggle">
<div class="toggle-handle"></div>
</div>
<!-- toggle -->
</li>
<div class="panel">
<p>Lorem ipsum dolor sit amet, consectetur adipisicing elit. Qui, officiis asperiores totam. Amet, beatae explicabo placeat consequatur repellendus quia rerum saepe cumque autem facilis! Perferendis, exercitationem eius vitae alias ad.</p>
</div>
 li.accordion {
curser: pointer;
padding: 18px;
width: 100%;
border: none;
text-align: left;
outline: none;
font-size: 15px;
transition: 0.4s;
}

li.accordion.active,
button.accordion:hover {
background-color: #ddd;
}

li.accordian:after {
content: '\002B';
color: #777;
font-weight: bold;
float: right;
margin-left: 5px;
}

div.panel {
padding: 0 18px;
background-color: white;
max-height: 0;
overflow: hidden;
transition: max-height 0.2s ease-out;
}

div.toggle {
z-index: 1;
}




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';
}
}
} //end function

最佳答案

你缺少的关键部分是 event.stopPropagation();因为这可以防止切换也触发 Accordion 。

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

for (i = 0; i < acc.length; i++) {
acc[i].getElementsByClassName("toggle")[0].onclick = function() {
event.stopPropagation();
this.classList.toggle("active");
}
acc[i].onclick = function() {
var panel = this.nextElementSibling;
if (panel.style.maxHeight) {
panel.style.maxHeight = null;
} else {
panel.style.maxHeight = panel.scrollHeight + 'px';
}
}
} //end function
li.accordion {
/* change the button tag to li tag */
curser: pointer;
padding: 18px;
width: 100%;
border: none;
text-align: left;
outline: none;
font-size: 15px;
transition: 0.4s;
}

li.accordion.active,
button.accordion:hover {
background-color: #ddd;
}

li.accordian:after {
content: '\002B';
color: #777;
font-weight: bold;
float: right;
margin-left: 5px;
}

div.panel {
padding: 0 18px;
background-color: white;
max-height: 0;
overflow: hidden;
transition: max-height 0.2s ease-out;
}

div.toggle {
z-index: 1;
}
<link href="https://cdnjs.cloudflare.com/ajax/libs/ratchet/2.0.2/css/ratchet.css" rel="stylesheet"/>
<li class="table-view-cell toggle-handle accordion">
Kitchen Light
<div class="toggle">
<div class="toggle-handle"></div>
</div>
<!-- toggle -->
</li>


<div class="panel">
<p>Lorem ipsum dolor sit amet, consectetur adipisicing elit. Qui, officiis asperiores totam. Amet, beatae explicabo placeat consequatur repellendus quia rerum saepe cumque autem facilis! Perferendis, exercitationem eius vitae alias ad.</p>
</div>
<!-- end panel -->

关于javascript - 定位 Accordion 中的切换按钮,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41859301/

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