You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

设置display:none的下拉菜单选项仍触发点击事件求助

问题解决:下拉菜单关闭后点击下方区域误触发选项事件

问题原因

下拉菜单关闭时,.options元素仅通过动画将透明度变为0,但元素本身仍存在于页面中,未设置隐藏或禁止点击,所以点击该区域时依然会触发选项的点击事件。当前CSS仅在.select-wrapper.active状态下给.options设置display: block,默认状态下.options没有设置display: none,仅靠动画将透明度降为0,元素的点击区域始终存在。

修复方案

方案1:默认隐藏.options

修改CSS,给默认状态的.options添加display: none,关闭时元素完全不占据空间、不接收点击:

.select-wrapper .options {
  /* 原有样式保留 */
  display: none; /* 添加此行 */
}

保持.select-wrapper.active .options的display: block不变,激活时显示菜单,关闭时隐藏。

方案2:关闭时阻止点击事件

若需保留元素占位(不推荐),可给非active状态的.options设置pointer-events: none,阻止点击事件传递:

.select-wrapper .options {
  /* 原有样式保留 */
  pointer-events: none; /* 添加此行 */
}

.select-wrapper.active .options {
  /* 原有样式保留 */
  pointer-events: auto; /* 激活时恢复点击 */
}

额外语义化修复

HTML中<ul>的子元素应为<li>而非<div>,修改后符合语义规范:

<ul class="options">
  <li class="option">
    <p class="option-text">Margin</p>
  </li>
  <li class="option">
    <p class="option-text">Test</p>
  </li>
  <li class="option">
    <p class="option-text">Fees</p>
  </li>
</ul>

修改后的完整代码

JavaScript

window.addEventListener("load", () => {
  const optionMenu = document.querySelector(".select-wrapper");
  const selectButton = optionMenu.querySelector(".select-button");
  const options = optionMenu.querySelectorAll(".option");
  const selectButtonText = optionMenu.querySelector(".select-button-text");

  selectButton.addEventListener("click", () =>
    optionMenu.classList.toggle("active")
  );

  options.forEach((option) => {
    option.addEventListener("click", () => {
      let selectedOption = option.querySelector(".option-text").innerText;
      selectButtonText.innerText = selectedOption;

      optionMenu.classList.remove("active");
    });
  });

  document.addEventListener("click", (event) => {
    if (!optionMenu.contains(event.target)) {
      optionMenu.classList.remove("active");
    }
  });
});

CSS

* {
  margin: 0;
  padding: 0;
  box-sizing: border-box;
  font-family: "Poppins", sans-serif;
}

body {
  background: #e3f2fd;
}

.select-wrapper {
  max-width: 330px;
  margin: 50px auto;
  position: relative; /* 让绝对定位的options相对于它定位 */
}
.select-wrapper .select-button {
  display: flex;
  height: 55px;
  background: #fff;
  padding: 20px;
  font-size: 18px;
  font-weight: 400;
  border-radius: 8px;
  align-items: center;
  cursor: pointer;
  justify-content: space-between;
  box-shadow: 0 0 5px rgba(0, 0, 0, 0.1);
}
.select-wrapper .options {
  position: absolute;
  width: 100%; /* 适配wrapper宽度 */
  overflow-y: auto;
  max-height: 295px;
  padding: 10px;
  margin-top: 10px;
  border-radius: 8px;
  background: #fff;
  box-shadow: 0 0 10px rgba(0, 0, 0, 0.1);
  display: none; /* 默认隐藏 */
  animation-name: fadeInDown;
  -webkit-animation-name: fadeInDown;
  animation-duration: 0.35s;
  animation-fill-mode: both;
  -webkit-animation-duration: 0.35s;
  -webkit-animation-fill-mode: both;
}
.select-wrapper .options .option {
  display: flex;
  height: 55px;
  cursor: pointer;
  padding: 0 16px;
  border-radius: 8px;
  align-items: center;
  background: #fff;
}
.select-wrapper .options .option:hover {
  background: #f2f2f2;
}
.select-wrapper .options .option i {
  font-size: 25px;
  margin-right: 12px;
}
.select-wrapper .options .option .option-text {
  font-size: 18px;
  color: #333;
}

.select-button i {
  font-size: 25px;
  transition: 0.3s;
}

.select-wrapper.active .select-button i {
  transform: rotate(-180deg);
}
.select-wrapper.active .options {
  display: block;
  opacity: 0;
  z-index: 10;
  animation-name: fadeInUp;
  -webkit-animation-name: fadeInUp;
  animation-duration: 0.4s;
  animation-fill-mode: both;
  -webkit-animation-duration: 0.4s;
  -webkit-animation-fill-mode: both;
}

@keyframes fadeInUp {
  from {
    transform: translate3d(0, 30px, 0);
    opacity: 0;
  }
  to {
    transform: translate3d(0, 0, 0);
    opacity: 1;
  }
}
@keyframes fadeInDown {
  from {
    transform: translate3d(0, 0, 0);
    opacity: 1;
  }
  to {
    transform: translate3d(0, 20px, 0);
    opacity: 0;
  }
}

HTML

<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8" />
  <meta name="viewport" content="width=device-width, initial-scale=1.0" />
  <meta http-equiv="X-UA-Compatible" content="ie=edge" />
  <title>Document</title>
  <link rel="stylesheet" type="text/css" href="/src/styles.css" />
  <link rel="stylesheet" href="https://unpkg.com/boxicons@2.1.1/css/boxicons.min.css" />
  <script src="/src/index.js"></script>
</head>

<body>

  <div class="select-wrapper">
    <div class="select-button">
      <div class="select-button-text">Select an option</div>
      <i class="bx bx-chevron-down"></i>
    </div>

    <ul class="options">
      <li class="option">
        <p class="option-text">Margin</p>
      </li>
      <li class="option">
        <p class="option-text">Test</p>
      </li>
      <li class="option">
        <p class="option-text">Fees</p>
      </li>
    </ul>
  </div>

</body>

</html>

内容的提问来源于stack exchange,提问作者treepek

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.27 12:17:04