首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何确保只有一个html元素是可见的

如何确保只有一个html元素是可见的
EN

Stack Overflow用户
提问于 2021-10-30 19:50:46
回答 1查看 41关注 0票数 0

我有两个列表,目前两个列表同时切换,我希望一次只能看到一个列表项,

如果我点击列表一,只列出一个项目应该是可见的,反之亦然。

代码

当前,这两个列表项目都显示在单击。

代码语言:javascript
复制
var dropdown = document.getElementsByClassName("dropdown-btn");
var i;

for (i = 0; i < dropdown.length; i++) {
  dropdown[i].addEventListener("click", function() {
    this.classList.toggle("active");
    var dropdownContent = this.nextElementSibling;
    if (dropdownContent.style.display === "block") {
      dropdownContent.style.display = "none";
    } else {
      dropdownContent.style.display = "block";
    }
  });
}
代码语言:javascript
复制
<div class=" dropdown-btn font-bold text-lg text-gray-700 ml-4 mt-3 cursor-pointer">LIST ONE</div>

<div class="hidden">
  <div class="cursor-pointer w-full     border-gray-100  items-start border-b-2 " style="height:40px">
    <div class="  text-base text-gray-700 ml-10 mt-7 ">option 1</a>
    </div>
  </div>
  <div class="w-full     border-gray-100  items-start border-b-2" style="height:40px">
    <div class="  text-base text-gray-700 ml-10 mt-3"> option 2</a>
    </div>
  </div>
</div>

<div class="w-full     border-gray-100  items-start border-b-2">
  <div class="dropdown-btn font-bold text-lg text-gray-700 ml-4 mt-8 cursor-pointer" style="height:40px">LIST TWO
  </div>

  <div class="hidden">

    <div class="w-full     border-gray-100  items-start border-b-2" style="height:40px">
      <div class="  text-base text-gray-700 ml-10 mt-1"><a href=>option 1</a></div>
    </div>
    <div class="w-full     border-gray-100  items-start border-b-2" style="height:40px">
      <div class="  text-base text-gray-700 ml-10 mt-3"><a href=>option 2</a></div>
    </div>


  </div>
</div>
</div>

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2021-10-30 20:06:40

您可以委托并切换已隐藏的内容。

注意,我在隐藏的div中添加了一个类。

代码语言:javascript
复制
const lists = document.querySelectorAll(".list")
const buttons = document.querySelectorAll(".dropdown-btn")
document.getElementById("container").addEventListener("click", function(e) {
  const tgt = e.target;
  if (tgt.classList.contains("dropdown-btn")) {
    tgt.classList.toggle("active");
    const thisList = tgt.nextElementSibling;
    const show = tgt.classList.contains('active')
    lists.forEach(list => list.classList.toggle("hidden", !show || list !=thisList))
    buttons.forEach(btn => btn.classList.toggle("active",btn === tgt && show))
  }
});
代码语言:javascript
复制
.hidden {
  display: none;
}

.active {
  color: red;
}
代码语言:javascript
复制
<div id="container">
  <div class=" dropdown-btn font-bold text-lg text-gray-700 ml-4 mt-3 cursor-pointer">LIST ONE</div>
  <div class="hidden list">
    <div class="cursor-pointer w-full border-gray-100 items-start border-b-2 " style="height:40px">
      <div class=" text-base text-gray-700 ml-10 mt-7 "><a href="">option 1</a>
      </div>
    </div>
    <div class="w-full border-gray-100 items-start border-b-2" style="height:40px">
      <div class="text-base text-gray-700 ml-10 mt-3"><a href="">option 2</a>
      </div>
    </div>
  </div>

  <div class="w-full border-gray-100 items-start border-b-2">
    <div class="dropdown-btn font-bold text-lg text-gray-700 ml-4 mt-8 cursor-pointer" style="height:40px">LIST TWO
    </div>

    <div class="hidden list">

      <div class="w-full border-gray-100 items-start border-b-2" style="height:40px">
        <div class=" text-base text-gray-700 ml-10 mt-1"><a href="">option 1</a></div>
      </div>
      <div class="w-full border-gray-100 items-start border-b-2" style="height:40px">
        <div class=" text-base text-gray-700 ml-10 mt-3"><a href="">option 2</a></div>
      </div>


    </div>
  </div>
</div>

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/69781642

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档