我添加了悬停并单击事件以打开下拉内容。添加悬停并单击事件到下拉按钮-Jquery
它作为工作的效果,但有轻微毛刺
悬停,内容是开放罚款和悬停隐藏。
在第一次点击它是开放罚款,但在第二次单击它并不会立即结束,我需要退出悬停状态,看第二次点击效果
HTML:
<html>
<head>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.1.1/jquery.min.js"></script>
<style>
.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);
padding: 12px 16px;
z-index: 1;
}
.dropdown:hover .dropdown-content {
display: block;
}
.openCont{
display:block !important;
}
.closeCont{
display: none !important;
}
</style>
</head>
<body>
<h2>Hoverable Dropdown</h2>
<p>Move the mouse over the text below to open the dropdown content.</p>
<div class="dropdown">
<button>Mouse over me</button>
<div class="dropdown-content">
<p>Hello World!</p>
</div>
</div>
<div class="dropdown">
<button>Mouse over me</button>
<div class="dropdown-content">
<p>Hello World!</p>
</div>
</div>
</body>
</html>
JS:
$(".dropdown").click(function(){
console.log('toggle')
$(this).find("div.dropdown-content").toggleClass('openCont');
});
当然重要像这样,因为你仍然是'徘徊'它说应该是可见的 – LGSon
为什么都使用'悬停'和'点击'? ...我的意思是,如果'click'应该正常工作,'hover'不会,反之亦然 – LGSon
@LGSon,实际上悬停只是为了一目了然地看到内容,但我有要求打开它点击使内容可见和第二次点击隐藏它 –