我试图在鼠标悬停和鼠标上显示/隐藏下一个子-NAV元素,但我无法获得Next()和Find()..的正确语法。。我尝试了:
$(this).next('.sub-nav').show();
$(this).next('div').find('.sub-nav').show();
都没有工作,我很确定我只是在使用错误的语法。我是否缺少某些东西,或者我需要提出其他警告才能让jQuery在上一个'has-children'div下找到下一个" sub-nav"?
$('.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>