我正在尝试创建FAQ手风琴,但是不幸的是,当其中一个选项卡打开时,只有一个空格,什么都没有显示。这是我正在使用的html,CSS和JavaScript。任何帮助将不胜感激。
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");
}
}
}
function setClass(els, className, fnName) {
for (var i = 0; i < els.length; i++) {
els[i].classList[fnName](className);
}
}
.master-accordion {
color: #141452;
width: 100%;
height: 80%;
padding: 0 0 0 8em;
position: relative;
z-index: 99;
align-items: center;
opacity: .9;
}
.accordion {
color: #141452;
font-weight: bold;
cursor: pointer;
padding: 18px;
width: 90%;
opacity: .95;
text-align: left;
border: 1px solid black;
transition: 0.4s;
font-family: 'Roboto', sans-serif;
z-index: 99;
}
.panel {
background-color: white;
display: none;
overflow: hidden;
width: 90%;
max-height: 0;
transition: max-height 0.2s ease-out;
position: relative;
z-index: 99;
font: 16px;
background-color: white;
padding: 10px 0 0 0;
}
.active,
.accordion:hover {
background-color: #141452;
color: white;
}
.accordion::after {
content: '\02795';
font-size: 15px;
float: right;
margin-left: 5px;
}
.active::after {
content: '\2796'
}
.accordion.active {
margin-bottom: 20px;
}
<div class="master-accordion">
<button class="accordion">HOW DO I CANCEL MY RENTED SPACE?</button>
<div class="panel">
If you wish to cancel your space, simply use the email contact form below to send us an email with your intent to cancel. We require 7 days notice before the end of the month in order to not be charged for an additional months rent.
</div>
<button class="accordion">DO YOU HAVE ANY COVERED PARKING SPACES?</button>
<div class="panel">
No, unfortunately not at this time
</div>
<button class="accordion">ARE THERE ANY DISCOUNTS FOR LONG TERM RENTING?</button>
<div class="panel">
Yes, you can signup and pay for a year ahead of time and receive the first month free. We are happy to discuss any longer terms, if you would like.
</div>
</div>
当我单击按钮时,它们将打开,但是当它们这样做时,将出现死角,这很可能是由于我在其上留有余量。我的JavaScript的全部功能都起作用,但是我看不到使我相信这是CSS问题的文本。
答案 0 :(得分:0)
您尚未为show
类设置任何属性。我删除了display:none
,并为max-height:150px
类设置了show
。
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");
}
}
}
function setClass(els, className, fnName) {
for (var i = 0; i < els.length; i++) {
els[i].classList[fnName](className);
}
}
.master-accordion {
color: #141452;
width: 100%;
height: 80%;
padding: 0 0 0 8em;
position: relative;
z-index: 99;
align-items: center;
opacity: .9;
}
.accordion {
color: #141452;
font-weight: bold;
cursor: pointer;
padding: 18px;
width: 90%;
opacity: .95;
text-align: left;
border: 1px solid black;
transition: 0.4s;
font-family: 'Roboto', sans-serif;
z-index: 99;
}
.panel {
background-color: white;
overflow: hidden;
width: 90%;
max-height: 0;
transition: max-height 0.2s ease-out;
position: relative;
z-index: 99;
font: 16px;
background-color: white;
padding: 10px 0 0 0;
}
.panel.show{
max-height: 150px;
}
.active,
.accordion:hover {
background-color: #141452;
color: white;
}
.accordion::after {
content: '\02795';
font-size: 15px;
float: right;
margin-left: 5px;
}
.active::after {
content: '\2796'
}
.accordion.active {
margin-bottom: 20px;
}
<div class="master-accordion">
<button class="accordion">HOW DO I CANCEL MY RENTED SPACE?</button>
<div class="panel">
If you wish to cancel your space, simply use the email contact form below to send us an email with your intent to cancel. We require 7 days notice before the end of the month in order to not be charged for an additional months rent.
</div>
<button class="accordion">DO YOU HAVE ANY COVERED PARKING SPACES?</button>
<div class="panel">
No, unfortunately not at this time
</div>
<button class="accordion">ARE THERE ANY DISCOUNTS FOR LONG TERM RENTING?</button>
<div class="panel">
Yes, you can signup and pay for a year ahead of time and receive the first month free. We are happy to discuss any longer terms, if you would like.
</div>
</div>