使用 jquery 将 ul 直接放在打开的类下
我有以下 jquery:
// Code for Side Navigation
$("#sideNav ul li:not(:has(li.current))")
.find("ul").hide().end() // Hide all other ULs
.hoverIntent(
function(){
$(this).children('ul').slideDown('fast');
},
function(){
// $(this).children('ul').slideUp('fast');
}
);
它上下滑动导航,使 current
类的任何 li 保持打开状态。 我想更改它,以便如果 li 有一个类 current
,则直接位于其下方的 ul 也将打开,但低于该级别的其他类将保持关闭状态。
HTML 结构如下:
<ul>
<li>Category 1
<ul>
<li class="current">Currently Open
<ul>
<li>Sub Cat 1 - should show
<ul><li>Should not Show</li></ul>
</li>
<li>Sub Cat 2 - should show</li>
</li>
</ul>
</li>
<li> Category 2
<ul><li>Should not Show></li></ul>
</li>
</ul>
I have the following bit o' jquery:
// Code for Side Navigation
$("#sideNav ul li:not(:has(li.current))")
.find("ul").hide().end() // Hide all other ULs
.hoverIntent(
function(){
$(this).children('ul').slideDown('fast');
},
function(){
// $(this).children('ul').slideUp('fast');
}
);
It slides a nav up and down, leaving any li with the class current
open.
I would like to change it so if an li has a class current
, the ul directly beneath it would be open too, but others below that level would remain closed.
Here is the HTML structure:
<ul>
<li>Category 1
<ul>
<li class="current">Currently Open
<ul>
<li>Sub Cat 1 - should show
<ul><li>Should not Show</li></ul>
</li>
<li>Sub Cat 2 - should show</li>
</li>
</ul>
</li>
<li> Category 2
<ul><li>Should not Show></li></ul>
</li>
</ul>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
首先,您需要修复您的 html。第二个嵌套 UL 未关闭。第二个是将当前路径中的所有 LI 元素分配给 .current 类。
然后使用这个 css
应该可以解决您的问题,而无需使用 javascript。这就是你所寻求的吗?
如果您需要添加悬停效果,您所需要做的就是将 .current 类添加到悬停的 li 中。
我添加了一些鼓舞人心的 jquery 代码(尚未测试),应该对您有帮助:
First off, you need to fix your html. the second nested UL isn't closed. The second is to assign all the LI elements in the current trail the .current class.
Then use this css
That should fix your problem without javascript. Is this what you seek?
If you need to add a hover effect all you need to do is add the .current class to li being hovered.
I have added some inspirational jquery code(haven't tested it) that should help you:
弄清楚:
添加
到 jquery 代码的底部。
Figure it out:
Added
to the bottom of the jquery code.