简体   繁体   English

打开另一个时如何关闭下拉菜单?

[英]How to close a drop-down while opening another?

I am new to javascript, I need help with my code. 我是javascript的新手,我的代码需要帮助。

I have two dropdowns that hold aa button and a container with links. 我有两个下拉列表,其中包含一个按钮和一个带链接的容器。 How do I make one dropdown close while I open the other? 当我打开另一个时,如何关闭一个下拉菜单?

I have tried comparing the giving them different ids and comparing them but I am not sure I got that right. 我试过比较给他们不同的ID并比较它们但我不确定我是否正确。

 // achieve effect // event delegation on body let activeDropDown; document.body.addEventListener('click', dropDown); // event function for toggling class function dropDown(ex) { // if (activeDropDown.id && activeDropDown.id !== ex.target.id) { // activeDropDown.nextElementSibling.classList.toggle('shower'); // } if(ex.target.parentElement.classList.contains('am')) { let val; activeDropDown = ex.target.parentElement.id; activeDropDown.element = ex.target.parentElement; val = activeDropDown; ex.target.nextElementSibling.classList.toggle('shower'); console.log(val); } } // close the dropdown if the user click outside the button window.addEventListener('click', closeDropDown); // declaring the function function closeDropDown(ex2) { if (!ex2.target.matches('.arch-button')) { // getting the dropdowncontent let postDrop = document.querySelectorAll('.monthly-post'); // var i; for (let i = 0; i < postDrop.length; i++) { let checkDropDown = postDrop[i]; if (checkDropDown.classList.contains('shower')) { checkDropDown.classList.remove('shower'); } } } }; 
  .am:not(:last-child) { border-bottom: 0.5px solid #C8C8C8; } .am:not(:first-child) { margin-top: 12px; } .monthly-post { position: relative; left: 17px; overflow: hidden; height: 0; } .shower{ overflow: visible !important; max-height: 100% !important; height: 100% !important; transition: all ease-in-out 500ms; -webkit-transition: all ease-in-out 500ms; -moz-transition: all ease-in-out 500ms; -ms-transition: all ease-in-out 500ms; -o-transition: all ease-in-out 500ms; } .post-linker { display: block; color: #0069E6; } .post-linker:hover, .post-linker:active{ color: #21293C; } 
  <div class="am" id="am-march"> <button class="arch-button">March 2019</button> <div class="monthly-post"> <a href="" class="post-linker">TEF Application 2019</a> <a href="" class="post-linker">Big Brother 2019</a> <a href="" class="post-linker">Hotelo new Application for guest</a> <a href="" class="post-linker">Air peace easter promo</a> </div> </div> <div class="am" id="am-april"> <button class="arch-button">April 2019</button> <div class="monthly-post"> <a href="" class="post-linker">ahahahah</a> <a href="" class="post-linker">ahahahah</a> <a href="" class="post-linker">ahahaha</a> <a href="" class="post-linker">ahahahahha</a> </div> </div> 
I want the dropdrop to close while I open another dropdown. 我希望在我打开另一个下拉列表时关闭dropdrop。

您可以使用event.pathevent.composedPath来检查元素是否不同,并关闭其他下拉列表。

Consume closeDropDown(); 消耗closeDropDown(); from dropDown() and remove click handlers. 从dropDown()中删除点击处理程序。

  let activeDropDown; document.body.addEventListener('click', dropDown); function dropDown(ex) { closeDropDown(); if (ex.target.parentElement.classList.contains('am')) { let val; activeDropDown = ex.target.parentElement.id; activeDropDown.element = ex.target.parentElement; val = activeDropDown; ex.target.nextElementSibling.classList.toggle('shower'); console.log(val); } } function closeDropDown() { let postDrop = document.querySelectorAll('.monthly-post'); for (let i = 0; i < postDrop.length; i++) { let checkDropDown = postDrop[i]; if (checkDropDown.classList.contains('shower')) { checkDropDown.classList.remove('shower'); } } }; 
  .am:not(:last-child) { border-bottom: 0.5px solid #C8C8C8; } .am:not(:first-child) { margin-top: 12px; } .monthly-post { position: relative; left: 17px; overflow: hidden; height: 0; } .shower { overflow: visible !important; max-height: 100% !important; height: 100% !important; transition: all ease-in-out 500ms; -webkit-transition: all ease-in-out 500ms; -moz-transition: all ease-in-out 500ms; -ms-transition: all ease-in-out 500ms; -o-transition: all ease-in-out 500ms; } .post-linker { display: block; color: #0069E6; } .post-linker:hover, .post-linker:active { color: #21293C; } 
 <div class="am" id="am-march"> <button class="arch-button">March 2019</button> <div class="monthly-post"> <a href="" class="post-linker">TEF Application 2019</a> <a href="" class="post-linker">Big Brother 2019</a> <a href="" class="post-linker">Hotelo new Application for guest</a> <a href="" class="post-linker">Air peace easter promo</a> </div> </div> <div class="am" id="am-april"> <button class="arch-button">April 2019</button> <div class="monthly-post"> <a href="" class="post-linker">ahahahah</a> <a href="" class="post-linker">ahahahah</a> <a href="" class="post-linker">ahahaha</a> <a href="" class="post-linker">ahahahahha</a> </div> </div> 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM