gpt4 book ai didi

javascript - 使用 JQuery next() 和 find() 在悬停的 div 上找到下一个最接近的类给定元素并显示它

转载 作者:太空宇宙 更新时间:2023-11-04 08:13:10 24 4
gpt4 key购买 nike

我试图在鼠标悬停和鼠标离开时显示/隐藏下一个子导航元素,但我无法获得 next() 和 find() 的正确语法...我已经尝试过:

$(this).next('.sub-nav').show();

$(this).next('div').find('.sub-nav').show();

两者都没有用,我很确定我只是使用了错误的语法。有没有我遗漏的东西,或者我需要为 Jquery 添加的另一个警告,以便在前面的“有 child ”div 下找到下一个“子导航”?

$('.has-children').hover(
function() {
$(this).css({
'border-bottom-right-radius': '0',
'border-bottom-left-radius': '0'
});
$('.bottom-nav .sub-nav li:last-child').css({
'border-bottom-right-radius': '10px',
'border-bottom-left-radius': '10px'
});

$('.sub-nav').show();
},
function() {
$(this).css({
'border-bottom-right-radius': '10px',
'border-bottom-left-radius': '10px'
});
$('.sub-nav').hide();
});
.bottom-nav {
position: relative;
z-index: 2;
max-width: 912px;
width: 100%;
margin-top: 22px;
}

.bottom-nav ul {
margin: 0;
padding: 0;
list-style-type: none;
}

.bottom-nav ul li {
display: inline-block;
background-color: #4ea45a;
border-bottom-left-radius: 10px;
border-bottom-right-radius: 10px;
height: 45px;
width: 225px;
font-size: 16px;
vertical-align: top;
margin-right: auto;
margin-left: auto;
text-align: center;
color: #fff;
line-height: 18px;
cursor: pointer;
}

.bottom-nav ul li a {
color: #fff;
}

.bottom-nav .sub-nav {
display: none;
margin-top: 15px;
padding: 0;
list-style-type: none;
width: 225px;
}

.bottom-nav .sub-nav li {
display: block;
height: 45px;
line-height: 15px;
background-color: #4ea45a;
border-bottom-left-radius: 0px;
border-bottom-right-radius: 0px;
font-size: 16px;
margin-right: auto;
margin-left: auto;
text-align: center;
color: #fff;
cursor: pointer;
}

.bottom-nav .sub-nav li img {
vertical-align: top;
}

.bottom-nav .sub-nav li a {
color: #fff;
}

.bottom-nav .sub-nav li a:hover,
.bottom-nav .sub-nav li a:active,
.bottom-nav ul li a:hover,
.bottom-nav ul li a:active {
text-decoration: none;
color: #ebd3af;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<div class="bottom-nav">
<ul>
<li class="has-children" style="padding-top:12px;">
<a href="about">MENU 1</a>
<ul class="sub-nav">
<li>
<a href="SUB">SUB 1</a>
</li>
</ul>
</li>
<li class="has-children" style="padding-top:3px;">
<a href="#">MENU 2</a>
<ul class="sub-nav">
<li>
<a href="#">SUB2</a>
</li>
</ul>
</li>
</ul>
</div>

最佳答案

没有 next(紧跟在 sibling 之后)是 div 或具有类 .sub-nav',因此两者都会失败。但是有一个 .sub-nav 子项,所以 find 会起作用。在这种情况下,.sub-nav.has-children 的子项,因此 .children() 也可以工作。

    $('.has-children').hover(
function() {
$(this).css({
'border-bottom-right-radius': '0',
'border-bottom-left-radius': '0'
})
.find('.sub-nav').show();

$('.bottom-nav .sub-nav li:last-child').css({
'border-bottom-right-radius': '10px',
'border-bottom-left-radius': '10px'
});
},
function() {
$(this).css({
'border-bottom-right-radius': '10px',
'border-bottom-left-radius': '10px'
})
.find('.sub-nav').hide();
});
.bottom-nav {
position: relative;
z-index: 2;
max-width: 912px;
width: 100%;
margin-top: 22px;
}

.bottom-nav ul {
margin: 0;
padding: 0;
list-style-type: none;
}

.bottom-nav ul li {
display: inline-block;
background-color: #4ea45a;
border-bottom-left-radius: 10px;
border-bottom-right-radius: 10px;
height: 45px;
width: 225px;
font-size: 16px;
vertical-align: top;
margin-right: auto;
margin-left: auto;
text-align: center;
color: #fff;
line-height: 18px;
cursor: pointer;
}

.bottom-nav ul li a {
color: #fff;
}

.bottom-nav .sub-nav {
display: none;
margin-top: 15px;
padding: 0;
list-style-type: none;
width: 225px;
}

.bottom-nav .sub-nav li {
display: block;
height: 45px;
line-height: 15px;
background-color: #4ea45a;
border-bottom-left-radius: 0px;
border-bottom-right-radius: 0px;
font-size: 16px;
margin-right: auto;
margin-left: auto;
text-align: center;
color: #fff;
cursor: pointer;
}

.bottom-nav .sub-nav li img {
vertical-align: top;
}

.bottom-nav .sub-nav li a {
color: #fff;
}

.bottom-nav .sub-nav li a:hover,
.bottom-nav .sub-nav li a:active,
.bottom-nav ul li a:hover,
.bottom-nav ul li a:active {
text-decoration: none;
color: #ebd3af;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<div class="bottom-nav">
<ul>
<li class="has-children" style="padding-top:12px;">
<a href="about">MENU 1</a>
<ul class="sub-nav">
<li>
<a href="SUB">SUB 1</a>
</li>
</ul>
</li>
<li class="has-children" style="padding-top:3px;">
<a href="#">MENU 2</a>
<ul class="sub-nav">
<li>
<a href="#">SUB2</a>
</li>
</ul>
</li>
</ul>
</div>

关于javascript - 使用 JQuery next() 和 find() 在悬停的 div 上找到下一个最接近的类给定元素并显示它,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46103931/

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