隐藏元素同时触发点击事件

Hide element and trigger click event simultaneously

我正在尝试构建一个简单的下拉菜单,当鼠标悬停在 div 上时会显示该菜单。当鼠标悬停在 div 或任何下拉元素上时,此下拉菜单应保持打开状态。正如您在下面的代码片段中看到的那样,我仅使用 CSS 完成了此操作。

但是,当用户单击其中一个下拉元素时,我预计会发生两件事:

  1. 下拉菜单是closed/hidden
  2. 点击事件被触发

我尝试使用 :active 伪类来隐藏下拉菜单。但如果我这样做,则不会触发点击事件。

如何既隐藏下拉菜单又触发点击事件?

function printConsole() { 
  console.log('Event triggered');
}

document.querySelectorAll('.shop').forEach(
  shop => shop.addEventListener('click', printConsole)
);
.shop-selector svg {
  padding: 2px;
  border-radius: 50%;
  background-color: orange;
  display: block;
  height: 2rem;
  width: 2rem;
}

.shop-selector svg:hover+.shop-dropdown {
  visibility: visible;
  opacity: 1;
}

.shop-selector .shop-dropdown {
  position: absolute;
  display: flex;
  flex-direction: column;
  justify-content: space-evenly;
  align-items: flex-start;
  z-index: 1;
  transition: all 0.7s;
  visibility: hidden;
  opacity: 0;
}

.shop-selector .shop-dropdown:hover {
  visibility: visible;
  opacity: 1;
}

.shop-selector .shop-dropdown:active {
  display: none;
}

.shop-selector .shop-dropdown .shop {
  margin: 5px 0 0;
  padding: 2px 5px;
  border-radius: 3px;
  background-color: orange;
}
<div class="shop-selector">
  <svg xmlns="http://www.w3.org/2000/svg" enable-background="new 0 0 24 24" viewBox="0 0 24 24" fill="black" width="18px" height="18px"><g><path d="M0,0h24v24H0V0z" fill="none"/><path d="M19.14,12.94c0.04-0.3,0.06-0.61,0.06-0.94c0-0.32-0.02-0.64-0.07-0.94l2.03-1.58c0.18-0.14,0.23-0.41,0.12-0.61 l-1.92-3.32c-0.12-0.22-0.37-0.29-0.59-0.22l-2.39,0.96c-0.5-0.38-1.03-0.7-1.62-0.94L14.4,2.81c-0.04-0.24-0.24-0.41-0.48-0.41 h-3.84c-0.24,0-0.43,0.17-0.47,0.41L9.25,5.35C8.66,5.59,8.12,5.92,7.63,6.29L5.24,5.33c-0.22-0.08-0.47,0-0.59,0.22L2.74,8.87 C2.62,9.08,2.66,9.34,2.86,9.48l2.03,1.58C4.84,11.36,4.8,11.69,4.8,12s0.02,0.64,0.07,0.94l-2.03,1.58 c-0.18,0.14-0.23,0.41-0.12,0.61l1.92,3.32c0.12,0.22,0.37,0.29,0.59,0.22l2.39-0.96c0.5,0.38,1.03,0.7,1.62,0.94l0.36,2.54 c0.05,0.24,0.24,0.41,0.48,0.41h3.84c0.24,0,0.44-0.17,0.47-0.41l0.36-2.54c0.59-0.24,1.13-0.56,1.62-0.94l2.39,0.96 c0.22,0.08,0.47,0,0.59-0.22l1.92-3.32c0.12-0.22,0.07-0.47-0.12-0.61L19.14,12.94z M12,15.6c-1.98,0-3.6-1.62-3.6-3.6 s1.62-3.6,3.6-3.6s3.6,1.62,3.6,3.6S13.98,15.6,12,15.6z"/></g></svg>
  <div class="shop-dropdown">
    <div class="shop">
      Shop Nameone
    </div>
    <div class="shop">
      Shop Nametwo
    </div>
    <div class="shop">
      Shop Namethree
    </div>
  </div>
</div>


点击不会work.Because悬停是通过鼠标完成的,因此,尝试使用“mousedown”之类的鼠标事件? ...让我们检查一下。

您是否尝试代理点击处理程序,并在菜单列表上使用切换 class 而不是对菜单项使用 :active? 我认为这应该是正确的方向。

我想这就是你需要的。

我删除了你的 css :active class defs 并在 shop-selector 这一行之上添加了一个自定义 .active class 只是为了显示您的下拉菜单 .shop-selector.active .shop-dropdown.

切换 .active class 和 printConsole => addAction

的小 js

const menu = document.getElementById('menu');
menu.addEventListener('mouseenter', e => {
  e.target.classList.add('active')
});

menu.addEventListener('mouseleave', e => {
  e.target.classList.remove('active')
});

// this is what you needed
function addAction() { 
  console.log('Event triggered');
  menu.classList.remove('active');
}

document.querySelectorAll('.shop').forEach(
  shop => shop.addEventListener('click', addAction )
);
.shop-selector svg {
  padding: 2px;
  border-radius: 50%;
  background-color: orange;
  display: block;
  height: 2rem;
  width: 2rem;
}

.shop-selector .shop-dropdown {
  position: absolute;
  display: flex;
  flex-direction: column;
  justify-content: space-evenly;
  align-items: flex-start;
  z-index: 1;
  transition: all 0.7s;
  visibility: hidden;
  opacity: 0;
}


.shop-selector.active .shop-dropdown {
  visibility: visible;
  opacity: 1;
}

.shop-selector .shop-dropdown .shop {
  margin: 5px 0 0;
  padding: 2px 5px;
  border-radius: 3px;
  background-color: orange;
}
<div class="shop-selector" id="menu">
  <svg xmlns="http://www.w3.org/2000/svg" enable-background="new 0 0 24 24" viewBox="0 0 24 24" fill="black" width="18px" height="18px"><g><path d="M0,0h24v24H0V0z" fill="none"/><path d="M19.14,12.94c0.04-0.3,0.06-0.61,0.06-0.94c0-0.32-0.02-0.64-0.07-0.94l2.03-1.58c0.18-0.14,0.23-0.41,0.12-0.61 l-1.92-3.32c-0.12-0.22-0.37-0.29-0.59-0.22l-2.39,0.96c-0.5-0.38-1.03-0.7-1.62-0.94L14.4,2.81c-0.04-0.24-0.24-0.41-0.48-0.41 h-3.84c-0.24,0-0.43,0.17-0.47,0.41L9.25,5.35C8.66,5.59,8.12,5.92,7.63,6.29L5.24,5.33c-0.22-0.08-0.47,0-0.59,0.22L2.74,8.87 C2.62,9.08,2.66,9.34,2.86,9.48l2.03,1.58C4.84,11.36,4.8,11.69,4.8,12s0.02,0.64,0.07,0.94l-2.03,1.58 c-0.18,0.14-0.23,0.41-0.12,0.61l1.92,3.32c0.12,0.22,0.37,0.29,0.59,0.22l2.39-0.96c0.5,0.38,1.03,0.7,1.62,0.94l0.36,2.54 c0.05,0.24,0.24,0.41,0.48,0.41h3.84c0.24,0,0.44-0.17,0.47-0.41l0.36-2.54c0.59-0.24,1.13-0.56,1.62-0.94l2.39,0.96 c0.22,0.08,0.47,0,0.59-0.22l1.92-3.32c0.12-0.22,0.07-0.47-0.12-0.61L19.14,12.94z M12,15.6c-1.98,0-3.6-1.62-3.6-3.6 s1.62-3.6,3.6-3.6s3.6,1.62,3.6,3.6S13.98,15.6,12,15.6z"/></g></svg>
  <div class="shop-dropdown">
    <div class="shop">
      Shop Nameone
    </div>
    <div class="shop">
      Shop Nametwo
    </div>
    <div class="shop">
      Shop Namethree
    </div>
  </div>
</div>