目前,当我点击子菜单项时,我的子菜单会显示在悬停和隐藏状态。这很棒。问题是,当我将鼠标悬停在顶级菜单上时,单击子菜单项后它不会显示子菜单。
我真的希望子菜单在用户点击子菜单项时隐藏,但如果用户再次悬停在顶级菜单上,我希望它再次显示。有想法该怎么解决这个吗?真的想要一个简单的JS或css解决方案,请不要jquery。
我确实尝试谷歌这一点,但我发现所有人都试图永久隐藏被点击的子菜单项。我还读到innerHTML将删除所有子元素,因此我将项目文本移动到按钮而不是li元素,并将其更改为innerText而不是innerHTML。
function classSelect(profession) {
document.getElementById('dropDownSelections').style.display = 'none';
document.getElementById('classMenuHeader').innerText = profession;
}
function toggleMenu() {
document.getElementById('dropDownSelections').style.display = 'block';
}
.dropDownList {
z-index: 1000;
overflow: visible;
position: absolute;
}
.dropDownList ul {
text-align: left;
display: inline;
margin: 0;
padding: 15px 4px 17px 0;
list-style: none;
-webkit-box-shadow: 0 0 5px rgba(0, 0, 0, 0.15);
-moz-box-shadow: 0 0 5px rgba(0, 0, 0, 0.15);
box-shadow: 0 0 5px rgba(0, 0, 0, 0.15);
overflow: visible;
}
.dropDownList ul li {
font: bold 12px/18px sans-serif;
display: inline-block;
margin-right: -4px;
position: relative;
padding: 15px 20px;
background: #fff;
cursor: pointer;
-webkit-transition: all 0.2s;
-moz-transition: all 0.2s;
-ms-transition: all 0.2s;
-o-transition: all 0.2s;
transition: all 0.2s;
overflow: visible;
}
.dropDownList ul li:hover {
background: #555;
color: #fff;
overflow: visible;
}
.dropDownList ul li ul {
padding: 0;
position: absolute;
top: 48px;
left: 0;
width: 150px;
-webkit-box-shadow: none;
-moz-box-shadow: none;
box-shadow: none;
display: none;
overflow: visible;
text-align: center;
}
.dropDownList ul li ul li {
background: #555;
display: block;
color: #fff;
text-shadow: 0 -1px 0 #000;
overflow: visible;
margin: 0;
padding: 0;
}
.dropDownList ul li ul li:hover {
background: #666;
}
.dropDownList ul li:hover ul {
display: block;
}
.dropDownList a {
text-decoration: none;
display: block;
height: 3em;
padding-top: 1.5em;
}
.dropdownList input[type="button"] {
margin: 0;
padding: 0;
border: 0;
background: transparent;
font-family: inherit;
font-size: 1em;
cursor: pointer;
}
<div class="dropDownList">
<ul>
<li onmoustover="toggleMenu();"><a href="#" id="classMenuHeader" onclick="return false;">Select Class</a>
<ul id="dropDownSelections">
<li onclick="classSelect('item1');">
<input type="button" value="item1" />
</li>
<li onclick="classSelect('item2');">
<input type="button" value="item2" />
</li>
<li onclick="classSelect('item3');">
<input type="button" value="item3" />
</li>
<li onclick="classSelect('item4');">
<input type="button" value="item4" />
</li>
<li onclick="classSelect('item5');">
<input type="button" value="item5" />
</li>
<li onclick="classSelect('item6');">
<input type="button" value="item6" />
</li>
<li onclick="classSelect('item7');">
<input type="button" value="item7" />
</li>
<li onclick="classSelect('item8');">
<input type="button" value="item8" />
</li>
</ul>
</li>
</ul>
</div>
答案 0 :(得分:0)
抱歉,我有点诅咒你的js和html,但这应该有效
HTML
<div class="dropDownList">
<ul id="classDropDownList">
<li><a href="#" id="classMenuHeader">Select Class</a>
<ul id="dropDownSelections">
<li onclick="classSelect('item1');">
<input type="button" value="item1" />
</li>
<li onclick="classSelect('item2');">
<input type="button" value="item2" />
</li>
<li onclick="classSelect('item3');">
<input type="button" value="item3" />
</li>
<li onclick="classSelect('item4');">
<input type="button" value="item4" />
</li>
<li onclick="classSelect('item5');">
<input type="button" value="item5" />
</li>
<li onclick="classSelect('item6');">
<input type="button" value="item6" />
</li>
<li onclick="classSelect('item7');">
<input type="button" value="item7" />
</li>
<li onclick="classSelect('item8');">
<input type="button" value="item8" />
</li>
</ul>
</li>
</ul>
</div>
的Javascript
document.addEventListener("DOMContentLoaded", function(event) {
document.getElementById('classDropDownList').onmouseover = function(){
document.getElementById('dropDownSelections').style.display = 'block';
}
document.getElementById('classDropDownList').onmouseout = function(){
document.getElementById('dropDownSelections').style.display = 'none';
}
});
function classSelect(profession) {
document.getElementById('dropDownSelections').style.display = 'none';
document.getElementById('classMenuHeader').innerText = profession;
}