gpt4 book ai didi

jquery - 如何在悬停在菜单项上时显示子菜单项

转载 作者:太空宇宙 更新时间:2023-11-03 22:13:33 26 4
gpt4 key购买 nike

这里有下拉菜单,其中有子菜单结构和类名,与父菜单结构相同。我试图在悬停在父菜单项上时显示子级菜单项,但我只能在一级菜单上显示它,但不能在二级菜单项上显示。

我做错了什么?

下面是我的例子中的代码。

<!DOCTYPE html>
<html>
<head>
<title></title>
</head>
<body>
<style type="text/css">

.we-mega-menu-submenu{
position: relative;
display: none;
}

</style>

<script src="https://code.jquery.com/jquery-2.2.4.min.js" integrity="sha256-BbhdlvQf/xTY9gja0Dq3HiwQF8LaCRTXxZKRutelT44=" crossorigin="anonymous"></script>
<script type="text/javascript">
$(document).ready(function(){
$('.we-mega-menu-li.dropdown-menu').mouseover(function(){
$(this).find('div.we-mega-menu-submenu:first').css({
"display":"block"
});
})

$('.we-mega-menu-li.dropdown-menu').mouseout(function(){
$(this).find('div.we-mega-menu-submenu:first').css({
"display":"none"
});
})

})
</script>

<ul class="nav-tabs">
<li class="we-mega-menu-li dropdown-menu">
<a href="#">Menu 1</a>
<div class="we-mega-menu-submenu">
<ul class="nav-tabs">
<li class="we-mega-menu-li">submenu1</li>
<li class="we-mega-menu-li dropdown-menu">submenu2</li>
<div class="we-mega-menu-submenu">
<ul class="nav-tabs">
<li>submenu1</li>
<li>submenu1</li>
<li>submenu1</li>
<li>submenu1</li>
<li>submenu1</li>
</ul>
</div>
<li class="we-mega-menu-li">submenu3</li>
<li class="we-mega-menu-li">submenu4</li>
<li class="we-mega-menu-li">submenu5</li>
</ul>
</div>
<li class="we-mega-menu-li"><a href="#">Menu 2</a></li>
<li class="we-mega-menu-li"><a href="#">Menu 3</a></li>
<li class="we-mega-menu-li dropdown-menu">
<a href="3">Menu 4</a>
<div class="we-mega-menu-submenu">
<ul class="nav-tabs">
<li class="we-mega-menu-li">submenu1</li>
<li class="we-mega-menu-li dropdown-menu">submenu2</li>
<div class="we-mega-menu-submenu">
<ul class="nav-tabs">
<li>submenu1</li>
<li>submenu2</li>
<li>submenu3</li>
<li>submenu4</li>
<li>submenu5</li>
</ul>
</div>
<li class="we-mega-menu-li">submenu3</li>
<li class="we-mega-menu-li">submenu4</li>
<li class="we-mega-menu-li">submenu5</li>
</ul>
</div>
</li>
<li class="we-mega-menu-li"><a href="#">Menu 5</a></li>
</ul>

</body>
</html>

顺便说一下codepen URL

https://codepen.io/Chandanay/pen/YzKJNEE

最佳答案

您需要将类"we-mega-menu-submenu" 的元素放入类"dropdown-menu"

的元素中

$(document).ready(function () {
$('.we-mega-menu-li.dropdown-menu').mouseover(function () {
$(this).find('div.we-mega-menu-submenu:first').css({
"display": "block"
});
})
$('.we-mega-menu-li.dropdown-menu').mouseout(function () {
$(this).find('div.we-mega-menu-submenu:first').css({
"display": "none"
});
})
})
.we-mega-menu-submenu{
position: relative;
display: none;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/1.11.3/jquery.min.js"></script>
<ul class="nav-tabs">
<li class="we-mega-menu-li dropdown-menu">
<a href="#">Menu 1</a>
<div class="we-mega-menu-submenu">
<ul class="nav-tabs">
<li class="we-mega-menu-li">submenu1</li>
<li class="we-mega-menu-li dropdown-menu">submenu2
<div class="we-mega-menu-submenu">
<ul class="nav-tabs">
<li>submenu1</li>
<li>submenu1</li>
<li>submenu1</li>
<li>submenu1</li>
<li>submenu1</li>
</ul>
</div>
</li>
<li class="we-mega-menu-li">submenu3</li>
<li class="we-mega-menu-li">submenu4</li>
<li class="we-mega-menu-li">submenu5</li>
</ul>
</div>
<li class="we-mega-menu-li"><a href="#">Menu 2</a></li>
<li class="we-mega-menu-li"><a href="#">Menu 3</a></li>
<li class="we-mega-menu-li dropdown-menu">
<a href="3">Menu 4</a>
<div class="we-mega-menu-submenu">
<ul class="nav-tabs">
<li class="we-mega-menu-li">submenu1</li>
<li class="we-mega-menu-li dropdown-menu">submenu2
<div class="we-mega-menu-submenu">
<ul class="nav-tabs">
<li>submenu1</li>
<li>submenu2</li>
<li>submenu3</li>
<li>submenu4</li>
<li>submenu5</li>
</ul>
</div>
</li>
<li class="we-mega-menu-li">submenu3</li>
<li class="we-mega-menu-li">submenu4</li>
<li class="we-mega-menu-li">submenu5</li>
</ul>
</div>
</li>
<li class="we-mega-menu-li"><a href="#">Menu 5</a></li>
</ul>

关于jquery - 如何在悬停在菜单项上时显示子菜单项,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57991483/

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