为什么自定义可折叠项在选项卡中无法正常工作?
Why Custom Collapsibles Don't Work Properly in Tabs?
我正在学习 this 教程,了解如何使用 html、css 和一些 javascript 使我自己的可折叠。到目前为止,我已经用这样的代码得到了我想要的输出:
var question = document.getElementsByClassName("question");
var i;
for (i = 0; i < question.length; i++) {
question[i].addEventListener("click", function() {
this.classList.toggle("active");
var answer = this.nextElementSibling.nextElementSibling;
if (answer.style.maxHeight){
answer.style.maxHeight = null;
} else {
answer.style.maxHeight = answer.scrollHeight + "px";
}
});
}
.faq {
width: 100%
}
.question {
background-color: transparent;
color: #329d9c;
cursor: pointer;
padding: 2% 10%;
width: 100%;
border: none;
text-align: left;
outline: none;
font-size: 18px;
font-weight: 700;
}
.question:after {
content: "6F"; /*up*/
font-size: 20px;
color: black;
float: left;
margin-left: -6%;
font-weight: 700;
transform: rotate(-90deg);
}
.active:after {
content: "6F"; /*down*/
font-size: 20px;
color: black;
float: left;
margin-left: -6%;
font-weight: 700;
transform: rotate(90deg);
}
.line {
width: 90%;
height: 1%;
background-color: #dddddd;
border-radius: 30px;
position: absolute;
margin-left: 10%;
}
.answer {
margin-left: 10%;
padding: 0 6%;
max-height: 0;
overflow: hidden;
transition: max-height 0.2s ease-out;
background-color: white;
box-shadow: 1px 2px #929191;
border: none;
border-radius: 0 0 10px 10px;
color: black;
}
<div class="faq">
<button class="question">Question 1</button>
<div class="line"></div>
<div class="answer">
Answer 1
</div>
<button class="question">Question 2</button>
<div class="line"></div>
<div class="answer">
Answer 2
</div>
</div>
我尝试 运行 相同的代码并在多个选项卡上制作可折叠项,所以它看起来或多或少像这样:
<div class="tabs">
<div class="tab-1">
<!-- the same code as snippet --!>
</div>
<div class="tab-2">
<!-- the same code as snippet --!>
</div>
</div>
但到目前为止还没有用。我尝试使用 bootstrap 3 来使用手风琴,但它弄乱了我的自定义 css 样式,所以我想知道有什么办法可以解决这个问题吗?
const topic = document.getElementsByClassName('topic');
const topicQS = document.querySelectorAll('.topic');
let topicQSArray = [];
//console.log('keysQS', Object.keys(topicQS));
for (let i = 0; i < topicQS.length; i++) {
topicQS[i].addEventListener('click', function showQuestionQS(e) {
this.classList.toggle('active');
this.nextElementSibling.classList.toggle('show');
let questionsQS = document.querySelectorAll('question');
});
}
const question = document.getElementsByClassName('question');
for (let i = 0; i < question.length; i++) {
question[i].addEventListener('click', function () {
this.classList.toggle('active');
const answer = this.nextElementSibling.nextElementSibling;
if (answer.style.maxHeight) {
answer.style.maxHeight = null;
} else {
answer.style.maxHeight = (20 + answer.scrollHeight) + 'px';
}
});
}
.topic {
background-color: transparent;
color: #329d9c;
cursor: pointer;
padding: 2% 10%;
width: 100%;
border: none;
text-align: left;
outline: none;
font-size: 18px;
font-weight: 700;
}
.topic::after {
content: '6F';
font-size: 20px;
color: black;
float: left;
margin-left: -6%;
font-weight: 700;
transform: rotate(-90deg);
}
.question {
position: relative;
left: 50px;
display: block;
background-color: transparent;
color: #329d9c;
cursor: pointer;
padding: 2% 10%;
width: 100%;
border: none;
text-align: left;
outline: none;
font-size: 18px;
font-weight: 700;
}
.question::after {
content: '6F';
font-size: 20px;
color: black;
float: left;
margin-left: -6%;
font-weight: 700;
transform: rotate(-90deg);
}
.question-container {
/* transition: all ease-in-out 0.3s; */
display: none;
}
.active::after {
content: '6F';
font-size: 20px;
color: black;
float: left;
margin-left: -6%;
font-weight: 700;
transform: rotate(90deg);
}
.line {
width: 90%;
height: 1%;
background-color: #dddddd;
border-radius: 30px;
position: absolute;
margin-left: 10%;
}
.answer {
margin-left: 10%;
margin-top: 1rem;
padding: 0 6%;
max-height: 0;
overflow: auto;
transition: max-height 0.2s ease-in-out;
background-color: white;
box-shadow: 1px 2px #929191;
border: none;
border-radius: 0 0 10px 10px;
color: black;
}
.show {
display: block;
}
<h1>Collapsed Q & A</h1>
<div class="topic1">
<button class="topic">Topic 1</button>
<div class="question-container">
<button class="question">Question 1</button>
<div class="line"></div>
<div class="answer">Answer 1 Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.</div>
<button class="question">Question 2</button>
<div class="line"></div>
<div class="answer">Answer 2 Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.</div>
<button class="question">Question 3</button>
<div class="line"></div>
<div class="answer">Answer 3 Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.</div>
</div>
</div>
<div class="topic2">
<button class="topic">Topic 2</button>
<div class="question-container">
<button class="question">Question 4</button>
<div class="line"></div>
<div class="answer">Answer 4</div>
<button class="question">Question 5</button>
<div class="line"></div>
<div class="answer">Answer 5</div>
<button class="question">Question 6</button>
<div class="line"></div>
<div class="answer">Answer 6</div>
</div>
</div>
<div class="topic3">
<button class="topic">Topic 3</button>
<div class="question-container">
<button class="question">Question 7</button>
<div class="line"></div>
<div class="answer">Answer 7</div>
<button class="question">Question 8</button>
<div class="line"></div>
<div class="answer">Answer 8</div>
<button class="question">Question 9</button>
<div class="line"></div>
<div class="answer">Answer 9</div>
</div>
</div>
我正在学习 this 教程,了解如何使用 html、css 和一些 javascript 使我自己的可折叠。到目前为止,我已经用这样的代码得到了我想要的输出:
var question = document.getElementsByClassName("question");
var i;
for (i = 0; i < question.length; i++) {
question[i].addEventListener("click", function() {
this.classList.toggle("active");
var answer = this.nextElementSibling.nextElementSibling;
if (answer.style.maxHeight){
answer.style.maxHeight = null;
} else {
answer.style.maxHeight = answer.scrollHeight + "px";
}
});
}
.faq {
width: 100%
}
.question {
background-color: transparent;
color: #329d9c;
cursor: pointer;
padding: 2% 10%;
width: 100%;
border: none;
text-align: left;
outline: none;
font-size: 18px;
font-weight: 700;
}
.question:after {
content: "6F"; /*up*/
font-size: 20px;
color: black;
float: left;
margin-left: -6%;
font-weight: 700;
transform: rotate(-90deg);
}
.active:after {
content: "6F"; /*down*/
font-size: 20px;
color: black;
float: left;
margin-left: -6%;
font-weight: 700;
transform: rotate(90deg);
}
.line {
width: 90%;
height: 1%;
background-color: #dddddd;
border-radius: 30px;
position: absolute;
margin-left: 10%;
}
.answer {
margin-left: 10%;
padding: 0 6%;
max-height: 0;
overflow: hidden;
transition: max-height 0.2s ease-out;
background-color: white;
box-shadow: 1px 2px #929191;
border: none;
border-radius: 0 0 10px 10px;
color: black;
}
<div class="faq">
<button class="question">Question 1</button>
<div class="line"></div>
<div class="answer">
Answer 1
</div>
<button class="question">Question 2</button>
<div class="line"></div>
<div class="answer">
Answer 2
</div>
</div>
我尝试 运行 相同的代码并在多个选项卡上制作可折叠项,所以它看起来或多或少像这样:
<div class="tabs">
<div class="tab-1">
<!-- the same code as snippet --!>
</div>
<div class="tab-2">
<!-- the same code as snippet --!>
</div>
</div>
但到目前为止还没有用。我尝试使用 bootstrap 3 来使用手风琴,但它弄乱了我的自定义 css 样式,所以我想知道有什么办法可以解决这个问题吗?
const topic = document.getElementsByClassName('topic');
const topicQS = document.querySelectorAll('.topic');
let topicQSArray = [];
//console.log('keysQS', Object.keys(topicQS));
for (let i = 0; i < topicQS.length; i++) {
topicQS[i].addEventListener('click', function showQuestionQS(e) {
this.classList.toggle('active');
this.nextElementSibling.classList.toggle('show');
let questionsQS = document.querySelectorAll('question');
});
}
const question = document.getElementsByClassName('question');
for (let i = 0; i < question.length; i++) {
question[i].addEventListener('click', function () {
this.classList.toggle('active');
const answer = this.nextElementSibling.nextElementSibling;
if (answer.style.maxHeight) {
answer.style.maxHeight = null;
} else {
answer.style.maxHeight = (20 + answer.scrollHeight) + 'px';
}
});
}
.topic {
background-color: transparent;
color: #329d9c;
cursor: pointer;
padding: 2% 10%;
width: 100%;
border: none;
text-align: left;
outline: none;
font-size: 18px;
font-weight: 700;
}
.topic::after {
content: '6F';
font-size: 20px;
color: black;
float: left;
margin-left: -6%;
font-weight: 700;
transform: rotate(-90deg);
}
.question {
position: relative;
left: 50px;
display: block;
background-color: transparent;
color: #329d9c;
cursor: pointer;
padding: 2% 10%;
width: 100%;
border: none;
text-align: left;
outline: none;
font-size: 18px;
font-weight: 700;
}
.question::after {
content: '6F';
font-size: 20px;
color: black;
float: left;
margin-left: -6%;
font-weight: 700;
transform: rotate(-90deg);
}
.question-container {
/* transition: all ease-in-out 0.3s; */
display: none;
}
.active::after {
content: '6F';
font-size: 20px;
color: black;
float: left;
margin-left: -6%;
font-weight: 700;
transform: rotate(90deg);
}
.line {
width: 90%;
height: 1%;
background-color: #dddddd;
border-radius: 30px;
position: absolute;
margin-left: 10%;
}
.answer {
margin-left: 10%;
margin-top: 1rem;
padding: 0 6%;
max-height: 0;
overflow: auto;
transition: max-height 0.2s ease-in-out;
background-color: white;
box-shadow: 1px 2px #929191;
border: none;
border-radius: 0 0 10px 10px;
color: black;
}
.show {
display: block;
}
<h1>Collapsed Q & A</h1>
<div class="topic1">
<button class="topic">Topic 1</button>
<div class="question-container">
<button class="question">Question 1</button>
<div class="line"></div>
<div class="answer">Answer 1 Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.</div>
<button class="question">Question 2</button>
<div class="line"></div>
<div class="answer">Answer 2 Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.</div>
<button class="question">Question 3</button>
<div class="line"></div>
<div class="answer">Answer 3 Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.</div>
</div>
</div>
<div class="topic2">
<button class="topic">Topic 2</button>
<div class="question-container">
<button class="question">Question 4</button>
<div class="line"></div>
<div class="answer">Answer 4</div>
<button class="question">Question 5</button>
<div class="line"></div>
<div class="answer">Answer 5</div>
<button class="question">Question 6</button>
<div class="line"></div>
<div class="answer">Answer 6</div>
</div>
</div>
<div class="topic3">
<button class="topic">Topic 3</button>
<div class="question-container">
<button class="question">Question 7</button>
<div class="line"></div>
<div class="answer">Answer 7</div>
<button class="question">Question 8</button>
<div class="line"></div>
<div class="answer">Answer 8</div>
<button class="question">Question 9</button>
<div class="line"></div>
<div class="answer">Answer 9</div>
</div>
</div>