未捕获的类型错误:此.accordioncontent未定义accordionopenclose http://localhost:5500/assets/js/app.js:36
我是一个超级基本的人请帮助我,这是我的代码的结构,我想当按钮被用户点击时, accordion 将显示和隐藏的内容,根据点击事件,默认情况下, accordion 内容设置为显示无,我认为有问题之间的dom遍历方法
html code
<div class="accordioncontainer">
<div class="accordioncontainerinner">
<div class="accordionopenclose">open close accordion</div>
<div class="accordioncontent">
<p class="accordiontext">Lorem ipsum dolor sit, amet consectetur adipisicing elit. Facere sint ex iusto eos dolores, asperiores dignissimos aut odio voluptatem quidem quaerat perspiciatis quasi ullam iure nesciunt odit nostrum non placeat.</p>
</div>
</div>
<div class="accordioncontainerinner">
<div class="accordionopenclose">open close accordion</div>
<div class="accordioncontent">
<p class="accordiontext">Lorem ipsum dolor sit, amet consectetur adipisicing elit. Facere sint ex iusto eos dolores, asperiores dignissimos aut odio voluptatem quidem quaerat perspiciatis quasi ullam iure nesciunt odit nostrum non placeat.</p>
</div>
</div>
<div class="accordioncontainerinner">
<div class="accordionopenclose">open close accordion</div>
<div class="accordioncontent">
<p class="accordiontext">Lorem ipsum dolor sit, amet consectetur adipisicing elit. Facere sint ex iusto eos dolores, asperiores dignissimos aut odio voluptatem quidem quaerat perspiciatis quasi ullam iure nesciunt odit nostrum non placeat.</p>
</div>
</div>
<div class="accordioncontainerinner">
<div class="accordionopenclose">open close accordion</div>
<div class="accordioncontent">
<p class="accordiontext">Lorem ipsum dolor sit, amet consectetur adipisicing elit. Facere sint ex iusto eos dolores, asperiores dignissimos aut odio voluptatem quidem quaerat perspiciatis quasi ullam iure nesciunt odit nostrum non placeat.</p>
</div>
</div>
<div class="accordioncontainerinner">
<div class="accordionopenclose">open close accordion</div>
<div class="accordioncontent">
<p class="accordiontext">Lorem ipsum dolor sit, amet consectetur adipisicing elit. Facere sint ex iusto eos dolores, asperiores dignissimos aut odio voluptatem quidem quaerat perspiciatis quasi ullam iure nesciunt odit nostrum non placeat.</p>
</div>
</div>
<div class="accordioncontainerinner">
<div class="accordionopenclose">open close accordion</div>
<div class="accordioncontent">
<p class="accordiontext">Lorem ipsum dolor sit, amet consectetur adipisicing elit. Facere sint ex iusto eos dolores, asperiores dignissimos aut odio voluptatem quidem quaerat perspiciatis quasi ullam iure nesciunt odit nostrum non placeat.</p>
</div>
</div>
</div>
css code
.accordioncontainer {
display: grid;
grid-template-columns: repeat(2, 1fr);
grid-template-rows: repeat(1, 1fr);
place-items: center;
grid-gap: 50px;
padding: 20px 150px;
}
.accordioncontainer .accordionopenclose {
background: transparent;
color: var(--primarycolor);
text-transform: capitalize;
/* font-weight: 600; */
font-size: 20px;
padding: 5px 10px;
border: 1px solid var(--primarycolor);
border-radius: 10px;
/* display: inline; */
text-align: center;
cursor: pointer;
transition: all 300ms ease;
}
.accordioncontainer .accordionopenclose:hover,
.accordioncontainer .accordionopenclose .active {
background: var(--primarycolor);
color: var(--backgroundcolor);
}
.accordioncontainerinner .accordioncontent {
padding: 10px;
display: none;
}
.accordioncontent p {
display: none;
}
javascript code
let accordionopenclose = document.getElementsByClassName("accordionopenclose");
let accordioncontent = document.getElementsByClassName("accordioncontent");
let xyz;
for (let xyz = 0; xyz < accordionopenclose.length; xyz++) {
accordionopenclose[xyz].addEventListener(
"click",
function accordionopenclose() {
this.classList.toggle("active");
accordioncontent = this.accordioncontent[xyz].parentElement.nextElementSibling.firstElementChild;
if (accordioncontent.style.display === "block") {
accordioncontent.style.display === "none";
} else {
accordioncontent.style.display === "block";
}
}
);
}
accordion 将打开和关闭当按钮被点击
1条答案
按热度按时间gblwokeq1#
在HTML代码中,accordion content元素的类名是“accordiontext”,而不是“accordioncontent”。因此,您需要相应地更新JavaScript代码。