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..
jquery:
$(document).ready(function () {
$('#nav .parent-item').click(function () {
$(this).children('ul').slideToggle();
});
});
the 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&g开发者_C百科t;
<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
There is no function (typo in the question)child
but children
&
You have to 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:
$('#nav .parent-item').click(function() {
$(this).children('ul').slideToggle();
return false;
});
jsfiddle: http://jsfiddle.net/QHr2r/1/
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;
});
});
精彩评论