3 回答
TA贡献1851条经验 获得超3个赞
$( document ).ready(function() {
debugger;
var ele=$(".dropdown-content");
$(".dropdown-content a").click(function(){
if(this.innerText=="Link 2"){
$(".dropdown-content").hide();
$(".dropbtn").hide();
}
});
});
.dropbtn {
background-color: #4CAF50;
color: white;
padding: 16px;
font-size: 16px;
border: none;
cursor: pointer;
}
.dropdown {
position: relative;
display: inline-block;
}
.dropdown-content {
display: none;
position: absolute;
background-color: #f9f9f9;
min-width: 160px;
box-shadow: 0px 8px 16px 0px rgba(0,0,0,0.2);
z-index: 1;
}
.dropdown-content a {
color: black;
padding: 12px 16px;
text-decoration: none;
display: block;
}
.dropdown-content a:hover {background-color: #f1f1f1}
.dropdown:hover .dropdown-content {
display: block;
}
.dropdown:hover .dropbtn {
background-color: #3e8e41;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<h2>Dropdown Menu</h2>
<p>Move the mouse over the button to open the dropdown menu.</p>
<div class="dropdown">
<button class="dropbtn">Dropdown</button>
<div class="dropdown-content">
<a href="#">Link 1</a>
<a href="#">Link 2</a>
<a href="#">Link 3</a>
</div>
</div>
你像这样隐藏一个下拉菜单,如果你想显示只需添加显示功能。
TA贡献1829条经验 获得超13个赞
试试这个切换方法应该有效
<a href="#" onclick="handleClick()" class="btn btn-floating" data-toggle="dropdown">
how/hide menu
</a>
function handleClick(){
$(".dropdown-menu").toggle();
}
请参考此链接以获取运行示例:
https://playcode.io/647558/
TA贡献1111条经验 获得超0个赞
只需从下拉列表中删除类“显示”:
// hide the menu when an example is clicked
$(".dropdown-item").on("click", function(el){
$(el.target).parents(".dropdown").removeClass("show");
});
添加回答
举报