简体   繁体   English

Select 仅 LI 项的 UL 子项

[英]Select only the UL child of a LI item

I want to do something similiar to a tree-view (really simpler)..我想做一些类似于树视图的事情(真的更简单)..

This is my effort: (When I click the first "parent-item it goes ok and reveals his "son", but when I click the son, which is also a "parent-item", it toggles back..这是我的努力:(当我单击第一个“父项”时,它可以正常显示他的“儿子”,但是当我单击也是“父项”的儿子时,它会切换回来..

So I want something like that closest() function but for childs instead of parents..所以我想要closest() function 但对于孩子而不是父母..

jquery: jquery:

$(document).ready(function () {
        $('#nav .parent-item').click(function () {
            $(this).children('ul').slideToggle();
        });
    });

the html: html:

<ul id="nav">
<li>Atividade Recente</li>
<li class="parent-item">Projetos</a>
    <ul>
        <li class="parent-item"><a href="#">Renavam</a>
            <ul>
                <li>Atividade Recente</li>
                <li>Conversação</li>
                <li>Tarefas</li>
                <li>Pessoa & Permissões</li>
            </ul>
        </li>
    </ul>
</li>
<li><a href="#">Minhas atividades</a></li>

Use利用

$(this).find('ul').first();

It finds all 'ul's below the context and the first() method limits it to just the first one它发现所有 'ul' 都在上下文之下,并且 first() 方法将其限制为第一个

Try this:尝试这个:

$(document).ready(function () {
        $('#nav .parent-item').click(function () {
            $(this).children('ul').slideToggle();
            return false;
        });
       $('#nav li:not(".parent-item")').click(function(){
         return false;
       });
    });

There is no function child but children & (typo in the question)没有 function child ,但有children & (问题中的错字

You have to return false;你必须return false; at the end of the function, otherwise the click will be bubbled up from children to their parents, that's why it appears to you as if the parent is toggling:在 function 的末尾,否则点击会从孩子冒泡到他们的父母,这就是为什么它看起来好像父母正在切换:

$('#nav .parent-item').click(function() {
    $(this).children('ul').slideToggle();
    return false;
});

jsfiddle: http://jsfiddle.net/QHr2r/1/ jsfiddle: http://jsfiddle.net/QHr2r/1/

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM