以下是我的下拉按钮的代码,其中包含1个额外级别。
当我点击按钮时,它工作正常,但是当我尝试在子菜单的下拉列表中单击时,我意识到出现了错误,我的四个项目只显示了最后一个子菜单。
.icon-cadet-left {
float: right;
}
.dropbtn {
width: 300px;
background-color: #4CAF50;
color: white;
padding: 16px;
font-family: 'Play';
font-size: 20px;
border: none;
cursor: pointer;
text-transform: uppercase;
letter-spacing: 2px;
}
.dropdown {
position: relative;
display: inline-block;
}
.dropdown-content {
display: none;
position: relative;
background-color: #f9f9f9;
min-width: 160px;
box-shadow: 0px 8px 16px 0px rgba(0, 0, 0, 0.2);
}
.dropdown-content a {
width: 300px;
color: black;
font-family: 'Play';
letter-spacing: 2px;
padding: 12px 16px;
text-decoration: none !important;
display: block;
text-align: center;
border-bottom: 0.1em solid #e1e1e1;
border-radius: 5%;
text-transform: uppercase;
overflow: hidden;
}
#dropdown-submenu {
display: none;
background-color: #f9f9f9;
position: absolute;
box-shadow: 0px 8px 16px 0px rgba(0, 0, 0, 0.2);
left: 100%;
top: 0px;
}
#dropdown-submenu a {
z-index: 1000;
width: 300px;
color: black;
font-family: 'Play';
letter-spacing: 2px;
padding: 12px 16px;
text-decoration: none !important;
display: block;
text-align: center;
border-bottom: 0.1em solid #e1e1e1;
border-radius: 5%;
text-transform: uppercase;
}
.dropdown-content a:hover {
background-color: #f1f1f1
}
#dropdown-submenu a:hover {
background-color: #f1f1f1
}
.dropdown:hover .dropdown-content {
display: block;
}
.dropdown-content:hover #dropdown-submenu {
display: block;
}
.dropdown:hover .dropbtn {
background-color: #3e8e41;
}

<div class="pricing-button">
<div class="dropdown">
<button class="dropbtn">Logo Design</button>
<div class="dropdown-content animated fadeIn">
<a class="span1" href="#">Logo & Identity<i class="fa fa-caret-right icon-cadet-left"></i></a>
<div id="dropdown-submenu">
<a>Logo Design</a>
<a>Business Cards</a>
<a>Sationary</a>
<a>Holiday Doodles</a>
</div>
<a class="hover2" href="#">Business & Advertising<i class="fa fa-caret-right icon-cadet-left"></i></a>
<div id="dropdown-submenu">
<a>Sample 01</a>
<a>Sample 02</a>
<a>Sample 03</a>
<a>Sample 04</a>
</div>
<a class="hover3" href="#">Website & Digitals<i class="fa fa-caret-right icon-cadet-left"></i></a>
<div id="dropdown-submenu">
<a>Sample 01</a>
<a>Sample 02</a>
<a>Sample 03</a>
<a>Sample 04</a>
</div>
<a class="hover4" href="#">Textile Designing<i class="fa fa-caret-right icon-cadet-left"></i></a>
<div id="dropdown-submenu">
<a>Sample 01</a>
<a>Sample 02</a>
<a>Sample 03</a>
<a>Sample 04</a>
</div>
</div>
</div>
</div>
&#13;
答案 0 :(得分:1)
所以这就是我在代码中改变的内容:
您的语法无效 - 您无法使用多个id
s-使用class
。
所以我将dropdown-submenu
更改为class
。
而不是.dropdown-content:hover #dropdown-submenu
使用此:
.dropdown-content a:hover + .dropdown-submenu {
display: block;
}
相邻的兄弟选择器(+
)选择紧跟菜单dropdown-submenu
标记后的a
。
让我知道您对此的反馈。谢谢!
.icon-cadet-left {
float: right;
}
.dropbtn {
width: 300px;
background-color: #4CAF50;
color: white;
padding: 16px;
font-family: 'Play';
font-size: 20px;
border: none;
cursor: pointer;
text-transform: uppercase;
letter-spacing: 2px;
}
.dropdown {
position: relative;
display: inline-block;
}
.dropdown-content {
display: none;
position: relative;
background-color: #f9f9f9;
min-width: 160px;
box-shadow: 0px 8px 16px 0px rgba(0, 0, 0, 0.2);
}
.dropdown-content a {
width: 300px;
color: black;
font-family: 'Play';
letter-spacing: 2px;
padding: 12px 16px;
text-decoration: none !important;
display: block;
text-align: center;
border-bottom: 0.1em solid #e1e1e1;
border-radius: 5%;
text-transform: uppercase;
overflow: hidden;
}
.dropdown-submenu {
display: none;
background-color: #f9f9f9;
position: absolute;
box-shadow: 0px 8px 16px 0px rgba(0, 0, 0, 0.2);
left: 100%;
top: 0px;
}
.dropdown-submenu a {
z-index: 1000;
width: 300px;
color: black;
font-family: 'Play';
letter-spacing: 2px;
padding: 12px 16px;
text-decoration: none !important;
display: block;
text-align: center;
border-bottom: 0.1em solid #e1e1e1;
border-radius: 5%;
text-transform: uppercase;
}
.dropdown-content a:hover {
background-color: #f1f1f1
}
.dropdown-submenu a:hover {
background-color: #f1f1f1
}
.dropdown:hover .dropdown-content {
display: block;
}
.dropdown-content a:hover + .dropdown-submenu {
display: block;
}
.dropdown:hover .dropbtn {
background-color: #3e8e41;
}
&#13;
<div class="pricing-button">
<div class="dropdown">
<button class="dropbtn">Logo Design</button>
<div class="dropdown-content animated fadeIn">
<a class="span1" href="#">Logo & Identity<i class="fa fa-caret-right icon-cadet-left"></i></a>
<div class="dropdown-submenu">
<a>Logo Design</a>
<a>Business Cards</a>
<a>Sationary</a>
<a>Holiday Doodles</a>
</div>
<a class="hover2" href="#">Business & Advertising<i class="fa fa-caret-right icon-cadet-left"></i></a>
<div class="dropdown-submenu">
<a>Sample 01</a>
<a>Sample 02</a>
<a>Sample 03</a>
<a>Sample 04</a>
</div>
<a class="hover3" href="#">Website & Digitals<i class="fa fa-caret-right icon-cadet-left"></i></a>
<div class="dropdown-submenu">
<a>Sample 01</a>
<a>Sample 02</a>
<a>Sample 03</a>
<a>Sample 04</a>
</div>
<a class="hover4" href="#">Textile Designing<i class="fa fa-caret-right icon-cadet-left"></i></a>
<div class="dropdown-submenu">
<a>Sample 01</a>
<a>Sample 02</a>
<a>Sample 03</a>
<a>Sample 04</a>
</div>
</div>
</div>
</div>
&#13;
答案 1 :(得分:0)
我看到你的代码有相同的id =&#34; dropdown-submenu&#34;好几次,但那不应该。 id属性指定HTML元素的唯一ID(该值在HTML文档中必须是唯一的)。如果您需要多次,那么您应该声明为class =&#34; dropdown-submenu&#34;然后你必须使用.dropdown-submenu而不是#dropdown-submenu。
请首先纠正您的ID相关错误,然后尝试。希望它应该有用。
谢谢!