简体   繁体   English

JavaScript如何关闭另一个下拉菜单时的下拉菜单。 没有jQuery

[英]JavaScript How to close one drop down when another one clicked to open. No Jquery

I have a lot of <ul> <li> s and every <ul> has it's own drop down. 我有很多<ul> <li> ,每个<ul>都有它自己的下拉菜单。 I want to have only one drop down open at a time. 我想一次只打开一个下拉列表。 If I click to open one, then the one which is already opened needs to be closed. 如果单击以打开一个,则需要关闭已打开的一个。

 function toggleClass(element, className) { if (!element || !className) { return; } var classString = element.className, nameIndex = classString.indexOf(className); if (nameIndex == -1) { classString += ' ' + className; } else { classString = classString.substr(0, nameIndex) + classString.substr(nameIndex + className.length); } element.className = classString; } function dropDown(el) { toggleClass(el.nextElementSibling, 'overlayOpen'); } 
 ul { width: 200px; } li { margin: 0; font-size: 20px; line-height: 50px; width: 100%; cursor: pointer; background-color: red; overflow: hidden; } .overlayOpen { opacity: 1 !important; height: 50px !important; width: 100% !important; } .overlay { width: 100%; z-index: 1000; background-color: green; overflow: hidden; max-width: 800px; opacity: 0; height: 0; width: 0; -webkit-transition: width 0.2s, height 0.2s, opacity 0.2s ease; -moz-transition: width 0.2s, height 0.2s, opacity 0.2s ease; -o-transition: width 0.2s, height 0.2s, opacity 0.2s ease; -ms-transition: width 0.2s, height 0.2s, opacity 0.2s ease; transition: width 0.2s, height 0.2s, opacity 0.2s ease; } 
 <ul> <li id="1" onclick='dropDown(this)'>example</li> <li id="overlay_1" class='overlay'>Hidden stuff</li> </ul> <ul> <li id="2" onclick='dropDown(this)'>example</li> <li id="overlay_2" class='overlay'>Hidden stuff</li> </ul> 

You could just remove the overlayOpen class from all elements prior to opening the clicked element: 您可以在打开clicked元素之前从所有元素中删除overlayOpen类:

function dropDown(el) {
  Array.from(document.querySelectorAll('.overlayOpen'))
       .filter(elem => elem !== el.nextElementSibling)
       .forEach(element => element.classList.remove('overlayOpen'));
  toggleClass(el.nextElementSibling, 'overlayOpen');
}

暂无
暂无

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

相关问题 打开另一个时如何关闭一个 Javascript 下拉菜单 - How to Close One Javascript Drop Down Menu When Opening Another 打开一个下拉列表,然后使用JavaScript关闭另一个 - Open one drop-down, close another with JavaScript jQuery切换:单击另一个时关闭所有打开的li - jQuery toggle: Close all open li when another one is clicked 如何在单击另一个面板时关闭所有打开的面板 - How to close all open panels when another one is clicked 当使用 Javascript 并且没有 jQuery 单击另一个菜单项的按钮(第一个的“堂兄”)时关闭一个菜单项的子菜单 - Close submenus of one menu item when another menu item's button ('cousin' of first) is clicked with Javascript and no jQuery jQuery滑动面板-打开另一个面板时关闭一个面板/如何仅打开一个面板? - jQuery sliding panels - close one panel when opening another one/How to open one panel only? 当在一个下拉列表中选择一个选项时,如何使用javascript在另一个下拉列表中显示其相关值 - when one option selected in one drop down list then how to display its related value in another drop down list using javascript 如何向下滑动一个标签并关闭其他所有打开的标签(JQuery / JavaScript) - How do I slide down one tab and close any others that are open (JQuery/JavaScript) 如何一次打开一个下拉? - How to open one drop down at one time? 引导程序单击另一个下拉列表时关闭下拉列表 - bootstrap close the drop down when clicked on another dropdown
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM