英文:
Javascript not opening/closing accordion
问题
我尝试了几种不同的方法,但无法确定为什么我的JS不能打开和关闭我的手风琴。请检查我的代码。您的帮助将不胜感激。
var acc = document.getElementsByClassName("accordion");
var i;
for (i = 0; i < acc.length; i++) {
acc[i].addEventListener("click", function() {
this.classList.toggle("active");
var panel = this.nextElementSibling;
if (panel.style.display === "block") {
panel.style.display = "none";
} else {
panel.style.display = "block";
}
});
}
<div class="wrap1">
<button class="accordion">About Us Page Map</button>
<div id="pan1" class="panel">
<p>
<a href="About.html" id="ppl">Personel section</a><br />
<a href="About.html" id="ach">Acheivments</a><br />
<a href="About.html" id="his">History</a><br />
</p>
</div>
</div>
body {
text-align: center;
background-color: #ffffff;
}
h1 {
font: 40px courier new;
text-align: center;
}
a {
font: 25px arial;
text-align:center;
}
.accordion {
background-color: #eee;
color: #444;
cursor: pointer;
padding: 18px;
width: 60%;
text-align: center;
border: none;
outline: none;
transition: 0.4s;
}
.active, .accordion:hover {
background-color: #ccc;
}
.panel {
padding: 0 18px;
background-color: white;
overflow: hidden;
font: 16px Source Sans Pro;
width:60%;
margin:0 auto;
display:none;
height:200px auto
}
p{
text-align:center;
}
我已添加了CSS,我确实有一个名为“accordion”的类,但在原始问题中没有显示,对不起!
英文:
I've tried a few different ways but can not tell why my.JS isn't opening and closing my accordion. Please check my code. Your help would be appreciated.
<!-- begin snippet: js hide: false console: true babel: false -->
<!-- language: lang-js -->
var acc = document.getElementsByClassName("accordion");
var i;
for (i = 0; i < acc.length; i++) {
acc[i].addEventListener("click", function() {
this.classList.toggle("active");
var panel = this.nextElementSibling;
if (panel.style.display === "block") {
panel.style.display = "none";
} else {
panel.style.display = "block";
}
});
}
/*this javascript seems to be fine and matches what some answers have said, and i've reset the page cache but i cannot seem to get it to work */
<!-- language: lang-html -->
<div class="wrap1">
<button class="accordion">About Us Page Map</button>
<div id="pan1" class="panel">
<p>
<a href="About.html" id="ppl">Personel section</a><br />
<a href="About.html" id="ach">Acheivments</a><br />
<a href="About.html" id="his">History</a><br />
</p>
</div>
</div>
<!-- language: lang-css -->
body {
text-align: center;
background-color: #ffffff;
}
h1 {
font: 40px courier new;
text-align: center;
}
a {
font: 25px arial;
text-align:center;
}
.accordion {
background-color: #eee;
color: #444;
cursor: pointer;
padding: 18px;
width: 60%;
text-align: center;
border: none;
outline: none;
transition: 0.4s;
}
.active, .accordion:hover {
background-color: #ccc;
}
.panel {
padding: 0 18px;
background-color: white;
overflow: hidden;
font: 16px Source Sans Pro;
width:60%;
margin:0 auto;
display:none;
height:200px auto
}
p{
text-align:center;
}
<!-- end snippet -->
I've added the css, i do have a class called accordion but it didn't show in the original question sorry!
答案1
得分: 1
问题在这里:
var acc = document.getElementsByClassName("accordion");
你的HTML文件中没有一个名为 "accordion" 的类,这就是为什么它无法识别它。你需要创建一个按钮,给它分配一个名为 "accordion" 的类,然后它就能正常工作了。
<button class="accordion">Personal</button>
<div class="panel">
<p>Personal here</p>
</div>
<button class="accordion">Achievements</button>
<div class="panel">
<p>Achievements here</p>
</div>
<button class="accordion">History</button>
<div class="panel">
<p>History here</p>
</div>
希望这有所帮助。
英文:
The problem is here:
var acc = document.getElementsByClassName("accordion");
You don't have "accordion" class in your HTML file, that's why it is not able to recognize it. You have to take a button, assign it a class named accordion and then it will work properly.
<!-- begin snippet: js hide: false console: true babel: false -->
<!-- language: lang-js -->
var acc = document.getElementsByClassName("accordion");
var i;
for (i = 0; i < acc.length; i++) {
acc[i].addEventListener("click", function() {
this.classList.toggle("active");
var panel = this.nextElementSibling;
if (panel.style.display === "block") {
panel.style.display = "none";
} else {
panel.style.display = "block";
}
});
}
<!-- language: lang-css -->
.accordion {
background-color: #eee;
color: #444;
cursor: pointer;
padding: 18px;
width: 100%;
border: none;
text-align: left;
outline: none;
font-size: 15px;
transition: 0.4s;
}
.active,
.accordion:hover {
background-color: #ccc;
}
.panel {
padding: 0 18px;
display: none;
background-color: white;
overflow: hidden;
}
<!-- language: lang-html -->
<button class="accordion">Personal</button>
<div class="panel">
<p>Personal here</p>
</div>
<button class="accordion">Achievements</button>
<div class="panel">
<p>Achievements here</p>
</div>
<button class="accordion">History</button>
<div class="panel">
<p>History here</p>
</div>
<!-- end snippet -->
Source: HTML and CSS Accordion - w3schools
Hope this helps.
答案2
得分: 0
我建议重构一些你的代码,以使其更易读,比如
HTML
<a href="#item1" class="accordion">项目1</a>
<div id="item1" class="accordion-content">这是我的折叠项1内容</div>
<a href="#item2" class="accordion">项目2</a>
<div id="item2" class="accordion-content">这是我的折叠项2内容</div>
JS
document.querySelectorAll(".accordion").forEach(function(node) {
node.addEventListener("click", function(e) {
this.nextElementSibling.toggleClass("active');
});
});
CSS
.accordion-content:not(.active) {
display: none;
}
除了重构之外,我无法看到一个具有类名 accordion
的元素。看起来你缺少包含实际 折叠项
的HTML。
英文:
I would suggest refactoring some of your code to make it more readable such as
HTML
<a href="#item1" class="accordion">Item 1</a>
<div id="item1" class="accordion-content">This is my accordion Item 1 content</div>
<a href="#item2" class="accordion">Item 2</a>
<div id="item2" class="accordion-content">This is my accordion Item 2 content</div>
JS
document.querySelectorAll(".accordion").forEach(function(node) {
node.addEventListener("click", function(e) {
this.nextElementSibling.toggleClass("active');
});
});
CSS
.accordion-content:not(.active) {
display: none;
}
Besides the refactor, I'm unable to see an element with the className accordion
. It seems you are missing the HTML containing the actual accordion
.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论