我的大脑检查了周末...
我正在寻找一种纯 Javascript 解决方案,如果在单击另一个主菜单项时打开一个下拉菜单框,则先前打开的下拉菜单将关闭,然后显示新单击的主菜单项的下拉菜单。我知道这可能很简单,但我想不出一个不复杂的解决方案。
此外,如果您在菜单项之外单击(文档中不是菜单项或下拉框的任何位置),应关闭所有打开的下拉菜单。
感谢您的帮助。
function testFunc(el) {
var parent = el.parentElement;
var dd = parent.lastChild.previousSibling;
dd.classList.toggle('show');
}
ul { list-style: none; margin: 0; padding: 0; }
ul li {
width: 100px;
float: left;
background: #dbdbdb;
line-height: 2em;
text-align: center;
margin: 0 5px;
cursor: pointer;
}
ul li span {
display: block;
}
ul li ul {
display: none;
}
.show {
display: block;
}
<ul>
<li>
<span onclick="testFunc(this)">Item 1</span>
<ul>
<li>Sub Item 1</li>
<li>Sub Item 2</li>
</ul>
</li>
<li>
<span onclick="testFunc(this)">Item 2</span>
<ul>
<li>Sub Item 1</li>
<li>Sub Item 2</li>
</ul>
</li>
<li>
<span onclick="testFunc(this)">Item 3</span>
<ul>
<li>Sub Item 1</li>
<li>Sub Item 2</li>
</ul>
</li>
<li>
<span onclick="testFunc(this)">Item 4</span>
<ul>
<li>Sub Item 1</li>
<li>Sub Item 2</li>
</ul>
</li>
</ul>
您可以将最后打开的菜单保存在函数外的变量
opened
中。然后,如果opened
不是null
,则单击菜单时,它将切换opened
(即隐藏上次打开的菜单)并切换单击的项目。
let opened = null
function testFunc(el) {
// gets the <ul> element of the clicked menu item
const menu = el.parentElement.lastChild.previousSibling;
if (!opened) {
// no menu item is shown
opened = menu
opened.classList.toggle('show');
} else if (menu == opened) {
// the clicked item is already showing
menu.classList.toggle('show')
opened = null
} else {
// the clicked item is hiddden but another one is showing
opened.classList.toggle('show')
opened = menu
opened.classList.toggle('show')
}
}
代码如下:
let opened = null
function testFunc(el) {
const menu = el.parentElement.lastChild.previousSibling;
if(!opened) {
opened = menu
opened.classList.toggle('show');
} else if(menu == opened) {
menu.classList.toggle('show')
opened = null
} else {
opened.classList.toggle('show')
opened = menu
opened.classList.toggle('show')
}
}
ul {
list-style: none;
margin: 0;
padding: 0;
}
ul li {
width: 100px;
float: left;
background: #dbdbdb;
line-height: 2em;
text-align: center;
margin: 0 5px;
cursor: pointer;
}
ul li span {
display: block;
}
ul li ul {
display: none;
}
.show {
display: block;
}
<ul>
<li>
<span onclick="testFunc(this)">Item 1</span>
<ul>
<li>Sub Item 1</li>
<li>Sub Item 2</li>
</ul>
</li>
<li>
<span onclick="testFunc(this)">Item 2</span>
<ul>
<li>Sub Item 1</li>
<li>Sub Item 2</li>
</ul>
</li>
<li>
<span onclick="testFunc(this)">Item 3</span>
<ul>
<li>Sub Item 1</li>
<li>Sub Item 2</li>
</ul>
</li>
<li>
<span onclick="testFunc(this)">Item 4</span>
<ul>
<li>Sub Item 1</li>
<li>Sub Item 2</li>
</ul>
</li>
</ul>
这里是一些 ES6 语法的变体,注意我已经更改了 HTML 命名结构以更好地维护代码,允许按类名调用元素
不必使用内联事件监听器
在一行中调用所有菜单项
这是 JavaScript 代码:
let opened = null
const toggleVisibility = e => e.classList.toggle('show')
const toggleDropDown = e => {
const clickedItem = e.target.parentElement.lastChild.previousSibling
toggleVisibility(clickedItem);
if (!opened) {
opened = clickedItem
} else if (opened == clickedItem) {
opened = null
} else {
toggleVisibility(opened);
opened = clickedItem
}
}
[...document.querySelectorAll('.dropDown')].forEach(dropDown => dropDown.addEventListener('click', toggleDropDown))
let opened = null
const toggleVisibility = e => e.classList.toggle('show')
const toggleDropDown = e => {
const clickedItem = e.target.parentElement.lastChild.previousSibling
toggleVisibility(clickedItem);
if (!opened) {
opened = clickedItem
} else if (opened == clickedItem) {
opened = null
} else {
toggleVisibility(opened);
opened = clickedItem
}
}
[...document.querySelectorAll('.dropDown')].forEach(dropDown => dropDown.addEventListener('click', toggleDropDown))
ul {
list-style: none;
margin: 0;
padding: 0;
}
ul li {
width: 100px;
float: left;
background: #dbdbdb;
line-height: 2em;
text-align: center;
margin: 0 5px;
cursor: pointer;
}
ul li span {
display: block;
}
ul li ul {
display: none;
}
.show {
display: block;
}
<ul>
<li>
<span class="dropDown">Item 1</span>
<ul>
<li>Sub Item 1</li>
<li>Sub Item 2</li>
</ul>
</li>
<li>
<span class="dropDown">Item 2</span>
<ul>
<li>Sub Item 1</li>
<li>Sub Item 2</li>
</ul>
</li>
<li>
<span class="dropDown">Item 3</span>
<ul>
<li>Sub Item 1</li>
<li>Sub Item 2</li>
</ul>
</li>
<li>
<span class="dropDown">Item 4</span>
<ul>
<li>Sub Item 1</li>
<li>Sub Item 2</li>
</ul>
</li>
</ul>
如果你想在用户点击菜单外部时关闭任何打开的菜单,你需要在文档本身上有一个事件监听器。因此,不是每个菜单按钮都有一个事件侦听器,您将有一个单独的事件侦听器来监视文档中发生的任何点击。
事件侦听器将确定单击的项目是否为菜单按钮,在这种情况下,它将运行菜单处理程序。否则它将关闭最后打开的菜单项。
JavaScript代码:
let opened = null
const toggleVisibility = e => e.classList.toggle('show')
const handleDropdown = e => {
const clickedItem = e.parentElement.lastChild.previousSibling
toggleVisibility(clickedItem)
if (!opened) {
opened = clickedItem
} else if (opened == clickedItem) {
opened = null
} else {
toggleVisibility(opened)
opened = clickedItem
}
}
const handleClick = e => {
if (e.target.className.includes('dropDown')) {
handleDropdown(e.target)
} else if (opened) {
toggleVisibility(opened)
opened = null
}
}
document.addEventListener('click', handleClick)
这里是完整的代码:
let opened = null
const toggleVisibility = e => e.classList.toggle('show')
const handleDropdown = e => {
const clickedItem = e.parentElement.lastChild.previousSibling
toggleVisibility(clickedItem)
if (!opened) {
opened = clickedItem
} else if (opened == clickedItem) {
opened = null
} else {
toggleVisibility(opened)
opened = clickedItem
}
}
const handleClick = e => {
if (e.target.className.includes('dropDown')) {
handleDropdown(e.target)
} else if (opened) {
toggleVisibility(opened)
opened = null
}
}
document.addEventListener('click', handleClick)
ul {
list-style: none;
margin: 0;
padding: 0;
}
ul li {
width: 100px;
float: left;
background: #dbdbdb;
line-height: 2em;
text-align: center;
margin: 0 5px;
cursor: pointer;
}
ul li span {
display: block;
}
ul li ul {
display: none;
}
.show {
display: block;
}
<ul>
<li>
<span class="dropDown">Item 1</span>
<ul>
<li>Sub Item 1</li>
<li>Sub Item 2</li>
</ul>
</li>
<li>
<span class="dropDown">Item 2</span>
<ul>
<li>Sub Item 1</li>
<li>Sub Item 2</li>
</ul>
</li>
<li>
<span class="dropDown">Item 3</span>
<ul>
<li>Sub Item 1</li>
<li>Sub Item 2</li>
</ul>
</li>
<li>
<span class="dropDown">Item 4</span>
<ul>
<li>Sub Item 1</li>
<li>Sub Item 2</li>
</ul>
</li>
</ul>
很难与 Ivan 的答案竞争,但这将是我的解决方案:
function Dropdown() {
// Listen to ALL (!) click events to also catch clicks OUTSIDE the dropdowns
document.addEventListener('click', function(e) {
if (e.target.closest('.dropdown')) {
closeOthers(e.target);
handleClick(e.target);
} else {
closeOthers(null);
}
});
// Add or remove 'expanded' CSS class, depending on the current situation
function handleClick(dropdown) {
if (dropdown.classList.contains('expanded')) {
dropdown.classList.remove('expanded');
} else {
dropdown.classList.add('expanded');
}
}
// Close all dropdowns except the one that gets passed as the element parameter
// Note that we may also pass null in order to close ALL dropdowns
function closeOthers(element) {
document.querySelectorAll('.dropdown > a').forEach(link => {
if (element != link) {
link.classList.remove('expanded');
}
});
}
}
document.addEventListener('DOMContentLoaded', Dropdown);
<div class="dropdown">
<a aria-label="Settings"></a>
<ul>
<li><a href="/account">Account</a></li>
<li><a href="/profile">Profile</a></li>
<li><a href="/tutorial">Tutorial</a></li>
</ul>
</div>
对我有用。不确定它是否可以为其他人工作。反馈表示赞赏。