[英]jQuery toggle children list-items
I want to toggle the visibility of single subitems by clicking on the parent list-item. 我想通过单击父列表项来切换单个子项的可见性。
I have a filetree like this: 我有这样的filetree:
<div id="browser">
<ul><li class='folder'>Musik
<ul><li class='folder'>Alben
<ul><li class='folder'>100JahreEAV
<ul>
<li class='file'>000_erste_allgemeine_verunsicherung_-_100_jahre_eav-backcover.jpg</li>
<li class='file'>000_erste_allgemeine_verunsicherung_-_100_jahre_eav-frontcover.jpg</li>
<li class='file'>101_alles_gute_eav.mp3</li><li class='file'>102_popstar.mp3</li>
<li class='file'>103_samurai.mp3</li><li class='file'>104_kuess_die_hand_schoene_frau.mp3</li>
<li class='file'>105_coconut_island.mp3</li><li class='file'>106_johnny_i_fahrscheine.mp3</li>
</ul>
</li></ul>
</li></ul>
</li></ul>
</div>
And this is my code so far: 到目前为止这是我的代码:
$("div#browser ul li ul").hide();
$("li").click(function() {
if($(this).hasClass('active')) {
$(this).removeClass('active').children().hide();
} else {
$(this).addClass('active').children().show();
}
});
The problem is that all parent list-items are affected if I click anywhere. 问题是如果我点击任何地方,所有父列表项都会受到影响。 Can you tell me how to toggle just one item at once?
你能告诉我如何一次切换一个项目吗?
Stop the propagation of clicks in inner list items. 停止内部列表项中的点击传播 。
$("li").on("click", function( event ) {
event.stopPropagation();
// rest of your logic here
});
When you click on a nested list item, the click event will not travel up the ancestor tree. 单击嵌套列表项时,单击事件将不会沿祖先树向上移动。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.