简体   繁体   English

为什么在javascript中单击外部时不关闭下拉菜单

[英]why not close dropdown when click outside in javascript

I have a dropdown in my design that should be deleted when clicked anywhere other than .categories.menu .我的设计中有一个下拉菜单,当单击.categories.menu以外的任何位置时应该将其删除。 That is, to lose its show class and close the .categories.menu .也就是说,失去它的表演类并关闭.categories.menu At the end of the code I wrote, if this section ( categoriesMenu.classList.remove ("show") ) is run, it will prevent the dropdown of the show class altogether.在我写的代码的最后,如果这个部分( categoriesMenu.classList.remove ("show") )被运行,它将完全阻止 show 类的下拉。 so I want to close the menu whenever a user clicks outside the menu.所以我想在用户点击菜单外时关闭菜单。

Thank you in advance for your cooperation.预先感谢您的合作。

 function toggleClass(elem, className) { if (elem.className.indexOf(className) !== -1) { elem.className = elem.className.replace(className, ''); } else { elem.className = elem.className.replace(/\s+/g, ' ') + ' ' + className; } return elem; } function toggleDisplay(elem) { const curDisplayStyle = elem.style.display; if (curDisplayStyle === 'none' || curDisplayStyle === '') { elem.style.display = 'block'; } else { elem.style.display = 'none'; } } function toggleMenuDisplay(e) { const dropdown = e.currentTarget.parentNode; const menu = dropdown.querySelector('.menu'); toggleClass(menu, 'show'); } function handleOptionSelected(e) { toggleClass(e.target.parentNode, 'show'); const id = e.target.id; const newValue = e.target.textContent + ' '; const titleElem = document.querySelector('.dropdown .dropdown--title'); titleElem.textContent = newValue; } const dropdownTitle = document.querySelector('.dropdown .dropdown--title'); const dropdownOptions = document.querySelectorAll('.dropdown .menu.categories a'); dropdownTitle.addEventListener('click', toggleMenuDisplay); dropdownOptions.forEach(item => item.addEventListener('click', handleOptionSelected)); var categoriesMenu = document.querySelector('.categories.menu'); window.addEventListener('click', function(event) { var categoriesMenuShow = categoriesMenu.classList.contains("show"); var isClickInsideElement = categoriesMenu.contains(event.target); if (isClickInsideElement == categoriesMenuShow ) { // categoriesMenu.classList.remove("show") } });
 .dropdown { position: relative; } .dropdown::before { content: "+"; position: absolute; width: 1.5rem; height: 1.5rem; top: 15px; right: 0; color: var(--cbl); } .dropdown .dropdown--title { padding: 0.75rem; width: 100%; cursor: pointer; } .dropdown .menu { cursor: pointer; max-height: 0; overflow: hidden; display: flex; flex-direction: column; position: absolute; z-index: 12; width: 100%; top: 45px; right: 0; background-color: var(--cwh); transition: max-height 0.3s; -webkit-transition: max-height 0.3s; -moz-transition: max-height 0.3s; -ms-transition: max-height 0.3s; -o-transition: max-height 0.3s; box-shadow: 0 3px 20px #ccc; -webkit-box-shadow: 0 3px 20px #ccc; -moz-box-shadow: 0 3px 20px #ccc; } .dropdown .menu.show { max-height: 20em !important; } .dropdown .menu.show a { color: var(--cbl); opacity: 1; transition: all 0.3s; -webkit-transition: all 0.3s; -moz-transition: all 0.3s; -ms-transition: all 0.3s; -o-transition: all 0.3s; transform: translateX(0); -webkit-transform: translateX(0); -moz-transform: translateX(0); -ms-transform: translateX(0); -o-transform: translateX(0); } .dropdown .menu a { padding: 1rem; opacity: 0; color: var(--cbl); transform: translateX(100%); -webkit-transform: translateX(100%); -moz-transform: translateX(100%); -ms-transform: translateX(100%); -o-transform: translateX(100%); } .dropdown .menu a:nth-child(1) { transition-delay: 0.2s; } .dropdown .menu a:nth-child(2) { transition-delay: 0.15s; } .dropdown .menu a:nth-child(3) { transition-delay: 0.1s; } .dropdown .menu a:nth-child(4) { transition-delay: 0.05s; } .dropdown .menu a:nth-child(5) { transition-delay: 0s; } .dropdown .menu a:not(:last-child) { border-bottom: 1px solid var(--cblo40); } .dropdown .menu a:hover { background: rgba(0, 0, 0, 0.2); }
 <div class="dropdown"> <div class="dropdown--title">Choose category</div> <div class="categories menu"> <a href="#" data-category="[15,16,26,27]" class="clicked">All</a> <a href="http://localhost/discount/product-category/other/" data-category="15">Other</a> <a href="http://localhost/discount/product-category/electronics/" data-category="16">Electronics</a> <a href="http://localhost/discount/product-category/sports/" data-category="26">Sports</a> <a href="http://localhost/discount/product-category/toys/" data-category="27">Toys &amp; Games</a> </div> </div>

Try this!尝试这个!

 function toggleClass(elem, className) { if (elem.className.indexOf(className) !== -1) { elem.className = elem.className.replace(className, ''); } else { elem.className = elem.className.replace(/\s+/g, ' ') + ' ' + className; } return elem; } function toggleDisplay(elem) { const curDisplayStyle = elem.style.display; if (curDisplayStyle === 'none' || curDisplayStyle === '') { elem.style.display = 'block'; } else { elem.style.display = 'none'; } } function toggleMenuDisplay(e) { const dropdown = e.currentTarget.parentNode; const menu = dropdown.querySelector('.menu'); toggleClass(menu, 'show'); } function handleOptionSelected(e) { toggleClass(e.target.parentNode, 'show'); const id = e.target.id; const newValue = e.target.textContent + ' '; const titleElem = document.querySelector('.dropdown .dropdown--title'); titleElem.textContent = newValue; } const dropdownTitle = document.querySelector('.dropdown .dropdown--title'); const dropdownOptions = document.querySelectorAll('.dropdown .menu.categories a'); dropdownTitle.addEventListener('click', toggleMenuDisplay); dropdownOptions.forEach(item => item.addEventListener('click', handleOptionSelected)); var categoriesMenu = document.querySelector('.categories.menu'); window.addEventListener('click', function(event) { var categoriesMenuShow = categoriesMenu.classList.contains("show"); var isClickInsideElement = categoriesMenu.contains(event.target); if (isClickInsideElement == categoriesMenuShow ) { // categoriesMenu.classList.remove("show") } }); //newly added code to determine outside clicks var ignoreClickOnMeElement = document.getElementById('dropdown'); document.addEventListener('click', function(event) { var isClickInsideElement = ignoreClickOnMeElement.contains(event.target); if (!isClickInsideElement) { categoriesMenu.classList.remove("show"); } });
 .dropdown { position: relative; } .dropdown::before { content: "+"; position: absolute; width: 1.5rem; height: 1.5rem; top: 15px; right: 0; color: var(--cbl); } .dropdown .dropdown--title { padding: 0.75rem; width: 100%; cursor: pointer; } .dropdown .menu { cursor: pointer; max-height: 0; overflow: hidden; display: flex; flex-direction: column; position: absolute; z-index: 12; width: 100%; top: 45px; right: 0; background-color: var(--cwh); transition: max-height 0.3s; -webkit-transition: max-height 0.3s; -moz-transition: max-height 0.3s; -ms-transition: max-height 0.3s; -o-transition: max-height 0.3s; box-shadow: 0 3px 20px #ccc; -webkit-box-shadow: 0 3px 20px #ccc; -moz-box-shadow: 0 3px 20px #ccc; } .dropdown .menu.show { max-height: 20em !important; } .dropdown .menu.show a { color: var(--cbl); opacity: 1; transition: all 0.3s; -webkit-transition: all 0.3s; -moz-transition: all 0.3s; -ms-transition: all 0.3s; -o-transition: all 0.3s; transform: translateX(0); -webkit-transform: translateX(0); -moz-transform: translateX(0); -ms-transform: translateX(0); -o-transform: translateX(0); } .dropdown .menu a { padding: 1rem; opacity: 0; color: var(--cbl); transform: translateX(100%); -webkit-transform: translateX(100%); -moz-transform: translateX(100%); -ms-transform: translateX(100%); -o-transform: translateX(100%); } .dropdown .menu a:nth-child(1) { transition-delay: 0.2s; } .dropdown .menu a:nth-child(2) { transition-delay: 0.15s; } .dropdown .menu a:nth-child(3) { transition-delay: 0.1s; } .dropdown .menu a:nth-child(4) { transition-delay: 0.05s; } .dropdown .menu a:nth-child(5) { transition-delay: 0s; } .dropdown .menu a:not(:last-child) { border-bottom: 1px solid var(--cblo40); } .dropdown .menu a:hover { background: rgba(0, 0, 0, 0.2); }
 <div class="dropdown" id="dropdown"> <div class="dropdown--title">Choose category</div> <div class="categories menu"> <a href="#" data-category="[15,16,26,27]" class="clicked">All</a> <a href="http://localhost/discount/product-category/other/" data-category="15">Other</a> <a href="http://localhost/discount/product-category/electronics/" data-category="16">Electronics</a> <a href="http://localhost/discount/product-category/sports/" data-category="26">Sports</a> <a href="http://localhost/discount/product-category/toys/" data-category="27">Toys &amp; Games</a> </div> </div>

Thanks and best regards!谢谢和最好的问候!

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

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