javascript - 使用多个下拉菜单切换无法按预期工作

标签 javascript css

我使用以下内容为多个 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/

相关文章:

javascript - 脚本未正确显示断线元素

html - 使用内联 block 时垂直对齐文本

asp.net - 禁用表单提交按钮

javascript - 如何在 Typescript 中使用默认值定义可选的构造函数参数

javascript - 尝试访问指令中的数据时 AngularJs 崩溃

javascript - 我无法访问注入(inject)的服务

html - 在图像链接上获得一些空间

html - 从 Firefox 加载外部 .CSS 文件

html - 水平对齐表格旁边的表格部分

css - 响应式布局中的相邻元素