如何只显示当前李的孩子并隐藏其他李子

Dip*_*pak 0 jquery parent-child jquery-selectors

我的页面中有多个嵌套的ul.嵌套的ul通过以下jquery代码显示.

jQuery的

$("li").click(function(){
    $(this).children("ul").show("slow");
});
Run Code Online (Sandbox Code Playgroud)

但是我想一次只显示一个嵌套的ul.我的意思是当我点击一个li时,它应该只显示其子ul并隐藏其他孩子ul.我在之前的jquery代码中添加了以下行

$("li ul").not(this).hide("slow");
Run Code Online (Sandbox Code Playgroud)

但它不起作用.

CSS

li ul{
    display:none;
}
Run Code Online (Sandbox Code Playgroud)

HTML

<li>
    <a href="#">Insert + </a>
    <ul>
        <li>
            <a href="services.php">Services</a>
        </li>
        <li>
            <a href="notice.php">Notice and Information</a>
        </li>
    </ul>
</li>

<li>
    <a href="#">View + </a>
    <ul>
        <li>
            <a href="services.php">Comments</a>
        </li>
        <li>
            <a href="notice.php">Status</a>
        </li>
    </ul>
</li>
Run Code Online (Sandbox Code Playgroud)

Ama*_*mal 7

试试这段代码

$("li").click(function(){
    $("li").not(this).children("ul").hide("slow");
    $(this).children("ul").show("slow");
});
Run Code Online (Sandbox Code Playgroud)

$("li").click(function(){
    $("li").not(this).children("ul").hide("slow");
    $(this).children("ul").show("slow");
});
Run Code Online (Sandbox Code Playgroud)
li ul{
    display:none;
}
Run Code Online (Sandbox Code Playgroud)
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.0/jquery.min.js"></script>
<li>
    <a href="#">Insert + </a>
    <ul>
        <li>
            <a href="services.php">Services</a>
        </li>
        <li>
            <a href="notice.php">Notice and Information</a>
        </li>
    </ul>
</li>

<li>
    <a href="#">View + </a>
    <ul>
        <li>
            <a href="services.php">Comments</a>
        </li>
        <li>
            <a href="notice.php">Status</a>
        </li>
    </ul>
</li>
Run Code Online (Sandbox Code Playgroud)