gpt4 book ai didi

javascript - 使悬停菜单停留足够长的时间来点击

转载 作者:行者123 更新时间:2023-11-28 00:07:24 25 4
gpt4 key购买 nike

我正在尝试创建一个带有子菜单的导航栏,但是当我打开子菜单时,我无法点击列表。悬停的列表不会保持打开状态,我无法点击它。

$('.one').hover(function(){
$(this).next().show();
}, function(){
$(this).next().hide();
});
body {
font-family: Arial, Helvetica, sans-serif;
}

.navbar {
overflow: hidden;
background-color: #333;
}

.navbar a {
float: left;
font-size: 16px;
color: white;
text-align: center;
padding: 14px 16px;
text-decoration: none;
}

.dropdown {
float: left;
overflow: hidden;
}

.dropdown .dropbtn {
font-size: 16px;
border: none;
outline: none;
color: white;
padding: 14px 16px;
background-color: inherit;
font-family: inherit;
margin: 0;
}

.navbar a:hover, .dropdown:hover .dropbtn {
background-color: red;
}

.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 {
float: none;
color: black;
padding: 12px 16px;
text-decoration: none;
display: block;
text-align: left;
}

.dropdown-content a:hover {
background-color: #ddd;
}

.dropdown:hover .dropdown-content {
display: block;
}

.yo {
display: none;
position: absolute;
background-color: #f1f1f1;
min-width: 160px;
box-shadow: 0px 8px 16px 0px rgba(0,0,0,0.2);
z-index: 1;
left: 100%;
top: 30%;
}

.yo a {
color: black;
padding: 12px 16px;
text-decoration: none;
display: block;
}
<!DOCTYPE html>
<html>
<head>
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/4.7.0/css/font-awesome.min.css">
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>

</head>
<body>

<div class="navbar">
<a href="#home">Home</a>
<a href="#news">News</a>
<div class="dropdown">
<button class="dropbtn">Dropdown
<i class="fa fa-caret-down"></i>
</button>
<div class="dropdown-content">
<a href="#" class="one">Link 1</a>
<div class="yo">
<a href="#">Link 1</a>
<a href="#">Link 2</a>
<a href="#">Link 3</a>
</div>
<a href="#" class="one">Link 2</a>
<div class="yo">
<a href="#">Link 4</a>
<a href="#">Link 5</a>
<a href="#">Link 6</a>
</div>
<a href="#" class="one">Link 3</a>
<div class="yo">
<a href="#">Link 7</a>
<a href="#">Link 8</a>
<a href="#">Link 9</a>
</div>
</div>
</div>
</div>

<h3>Dropdown Menu inside a Navigation Bar</h3>
<p>Hover over the "Dropdown" link to see the dropdown menu.</p>

</body>
</html>

当我点击下拉菜单时 --> 将鼠标悬停在Link 1 上,现在我想选择 <悬停时打开的子子菜单中的 strong>Link 1。无论如何我可以保持链接打开足够长的时间来点击链接。

最佳答案

请注意“下拉”链接如何在其 DIV 中包含所有内容,并且仅通过 css :hover 显示/隐藏菜单。您需要将按钮“one”和子菜单“yo”都放在一个 div 中,然后执行 css :hover 在 div 上。不需要 jquery。

html:

<div class="navbar">
<a href="#home">Home</a>
<a href="#news">News</a>
<div class="dropdown">
<button class="dropbtn">Dropdown
<i class="fa fa-caret-down"></i>
</button>
<div class="dropdown-content">
<div class="submenu">
<a href="#" class="one">Link 1</a>
<div class="yo">
<a href="#">Link 1</a>
<a href="#">Link 2</a>
<a href="#">Link 3</a>
</div>
</div>
<div class="submenu">
<a href="#" class="one">Link 2</a>
<div class="yo">
<a href="#">Link 4</a>
<a href="#">Link 5</a>
<a href="#">Link 6</a>
</div>
</div>
<div class="submenu">
<a href="#" class="one">Link 3</a>
<div class="yo">
<a href="#">Link 7</a>
<a href="#">Link 8</a>
<a href="#">Link 9</a>
</div>
</div>
</div>
</div>
</div>

添加这个CSS:

.submenu:hover .yo {
display: block;
}

移除 JQuery 悬停。

Here is the working fiddle

关于javascript - 使悬停菜单停留足够长的时间来点击,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55621680/

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