gpt4 book ai didi

jquery - 为下拉按钮添加Hover和点击事件-Jquery

转载 作者:行者123 更新时间:2023-11-28 04:18:53 24 4
gpt4 key购买 nike

我添加了悬停和点击事件来打开下拉内容。

效果正常,但有小故障

悬停时,内容正常打开,悬停时隐藏。

第一次点击打开正常,第二次点击没有立即关闭,需要退出悬停状态才能看到第二次点击效果

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');

});

最佳答案

如果你这样做它会工作,添加一个 mouseout 处理程序

$(".dropdown").mouseout(function(){
$(this).find("div.dropdown-content").removeClass('closeCont');
});

更新你的 JS

$(this).find("div.dropdown-content").toggleClass('openCont closeCont');
if ($(this).is(":hover")) {
$(this).find("div.dropdown-content").addClass('closeCont');
}

通过更改此规则更新您的 CSS

.dropdown:hover .dropdown-content:not(.closeCont) {
display: block;
}

交换这 2 条规则的顺序,使 openCont 在最后(并删除 !important)

.closeCont{
display: none;
}
.openCont{
display:block;
}

它的工作原理是在您单击隐藏它之后,它会隐藏,但是一旦您将鼠标移到外面,它也会删除 closeCont,这将使它在悬停时再次工作

堆栈片段

$(".dropdown").click(function(){
console.log('toggle')
$(this).find("div.dropdown-content").toggleClass('openCont closeCont');
if ($(this).is(":hover")) {
$(this).find("div.dropdown-content").addClass('closeCont');
}
});

$(".dropdown").mouseout(function(){
$(this).find("div.dropdown-content").removeClass('closeCont');
});
.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:not(.closeCont) {
display: block;
}
.closeCont{
display: none;
}
.openCont{
display:block;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.1.1/jquery.min.js"></script>

<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>

关于jquery - 为下拉按钮添加Hover和点击事件-Jquery,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42321688/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com