gpt4 book ai didi

javascript - 使用多个下拉切换不按预期工作

转载 作者:行者123 更新时间:2023-11-29 18:43:12 24 4
gpt4 key购买 nike

我正在使用以下内容为多个 div 生成下拉字段,但就目前而言,每个生成的切换按钮只会打开第一个 div 的切换内容。

这是我目前使用的:

.toggle {
display: none;
}

.option {
position: relative;
margin-bottom: 1em;
}

.bio-title,
.bio-content {
backface-visibility: hidden;
transform: translateZ(0);
transition: all 0.2s;
}

.bio-title {
background: #fff;
padding: 1em;
display: block;
color: #7A7572;
font-weight: bold;
}

.bio-title:after,
.bio-title:before {
content: "";
position: absolute;
right: 1.25em;
top: 1.25em;
width: 2px;
height: 0.75em;
background-color: #7A7572;
transition: all 0.2s;
}

.bio-title:after {
transform: rotate(90deg);
}

.bio-content {
max-height: 0;
overflow: hidden;
background-color: #fff;
}

.bio-content p {
margin: 0;
padding: 0.5em 1em 1em;
font-size: 0.9em;
line-height: 1.5;
}

.toggle:checked+.bio-title,
.toggle:checked+.bio-title+.bio-content {
box-shadow: 3px 3px 6px #ddd, -3px 3px 6px #ddd;
}

.toggle:checked+.bio-title+.bio-content {
max-height: 500px;
}

.toggle:checked+.bio-title:before {
transform: rotate(90deg) !important;
}
<div class="option">
<input type="checkbox" id="toggle" class="toggle" />
<label class="bio-title" for="toggle">
Learn more
</label>
<div class="bio-content">
<p>Content</p>
</div>
</div>

<div class="option">
<input type="checkbox" id="toggle" class="toggle" />
<label class="bio-title" for="toggle">
Learn more
</label>
<div class="bio-content">
<p>Content</p>
</div>
</div>

我愿意使用 JavaScript 之类的东西,但最好只保留此 CSS。后者有可能吗?

最佳答案

最简单的解决方案是更改 ID。

.toggle {
display: none;
}

.option {
position: relative;
margin-bottom: 1em;
}

.bio-content {
max-height: 0;
overflow: hidden;
background-color: #fff;
transition: max-height 1s ease;
}

.toggle:checked+.bio-title+.bio-content {
max-height: 500px;
}
<div class="option">
<input type="checkbox" id="toggle" class="toggle" />
<label class="bio-title" for="toggle">
Learn more
</label>
<div class="bio-content">
<p>Content</p>
</div>
</div>

<div class="option">
<input type="checkbox" id="toggle2" class="toggle" />
<label class="bio-title" for="toggle2">
Learn more
</label>
<div class="bio-content">
<p>Content</p>
</div>
</div>

在您的情况下 - 由于嵌套的 DOM 选择器 (还) 不受支持 - 您可以使用一个小的 JS 片段来实现相同的结果,而无需 ID。

function toggleTab(el) {
// Get the first .toggle element of the parent
const checkbox = el.parentNode.querySelector(':scope > .toggle');
// Toggle the checked state
checkbox.checked = !checkbox.checked;
}
.toggle {
display: none;
}

.option {
position: relative;
margin-bottom: 1em;
}

.bio-content {
max-height: 0;
overflow: hidden;
background-color: #fff;
transition: max-height 1s ease;
}

.toggle:checked+.bio-title+.bio-content {
max-height: 500px;
}
<div class="option">
<input type="checkbox" class="toggle" />
<label class="bio-title" onclick="toggleTab(this)">
Learn more
</label>
<div class="bio-content">
<p>Content</p>
</div>
</div>

<div class="option">
<input type="checkbox" class="toggle" />
<label class="bio-title" onclick="toggleTab(this)">
Learn more
</label>
<div class="bio-content">
<p>Content</p>
</div>
</div>

关于javascript - 使用多个下拉切换不按预期工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55577273/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com