使用 jquery 将 ul 直接放在打开的类下

发布于 2024-09-15 20:14:22 字数 1096 浏览 2 评论 0原文

我有以下 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

国产ˉ祖宗 2024-09-22 20:14:23

首先,您需要修复您的 html。第二个嵌套 UL 未关闭。第二个是将当前路径中的所有 LI 元素分配给 .current 类。

<ul>
<li class="current">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> 
            </ul>
        </li>
    </ul>
</li>
<li> Category 2
    <ul><li>Should not Show</li></ul>
</li>

然后使用这个 css

    li.current>ul{
    display : block;
}

li ul{
    display : none;
}

应该可以解决您的问题,而无需使用 javascript。这就是你所寻求的吗?

如果您需要添加悬停效果,您所需要做的就是将 .current 类添加到悬停的 li 中。

我添加了一些鼓舞人心的 jquery 代码(尚未测试),应该对您有帮助:

    $('li:has(ul)').bind('mousein',function(){
    $(this).find('ul').slideDown();
});
$('li:has(ul)').bind('mouseout',function(){
    $(this).find('ul').slideUp();
});

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.

<ul>
<li class="current">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> 
            </ul>
        </li>
    </ul>
</li>
<li> Category 2
    <ul><li>Should not Show</li></ul>
</li>

Then use this css

    li.current>ul{
    display : block;
}

li ul{
    display : none;
}

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:

    $('li:has(ul)').bind('mousein',function(){
    $(this).find('ul').slideDown();
});
$('li:has(ul)').bind('mouseout',function(){
    $(this).find('ul').slideUp();
});
阪姬 2024-09-22 20:14:22

弄清楚:

添加

$("#sideNav ul li.topCurrent").find("ul").show();
$("#sideNav ul li.topCurrent").find("ul li:not(:has(li.current)) ul").hide();

到 jquery 代码的底部。

Figure it out:

Added

$("#sideNav ul li.topCurrent").find("ul").show();
$("#sideNav ul li.topCurrent").find("ul li:not(:has(li.current)) ul").hide();

to the bottom of the jquery code.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文