我有一个简单的手风琴, 我想显示/隐藏来自 div 的点击内容.到目前为止,我实现了我的代码添加活动类并且运行良好,问题在于这个类。控制台给我一个错误,showUncaught TypeError: Cannot read property 'classList' of null
这是我的代码
var acc = document.getElementsByClassName("accordion");
var panel = document.getElementsByClassName('panel');
for (var i = 0; i < acc.length; i++) {
acc[i].onclick = function() {
var setClasses = !this.classList.contains('active');
setClass(acc, 'active', 'remove');
setClass(panel, 'show', 'remove');
if (setClasses) {
this.classList.toggle("active");
this.nextElementSibling.classList.toggle("show", 800);
}
}
}
function setClass(els, className, fnName) {
for (var i = 0; i < els.length; i++) {
els[i].classList[fnName](className);
}
}
div.panel {
display: none;
transition: 4.6 ease-in-out;
overflow: hidden;
}
div.panel.show {
display: block !important;
<p><button class="accordion">▼ Title 1</button></p>
<div class="panel">
<ul>
<li>
Title
<ul>
<li>Lorem</li>
<li>aaaaa</li>
</ul>
</ul>
</div>
<p><button class="accordion">▼ Title 2</button></p>
<div class="panel">
<ul>
<li>
Title
<ul>
<li>Lorem 1</li>
<li>aaaaa</li>
</ul>
</ul>
</div>
有人可以尝试帮助我吗?谢谢大家
梵蒂冈之花
HUX布斯
相关分类