在我的模板中,我使用了与 this question. 中相同的树结构代码该问题的答案打开了树完全折叠的页面(jsfiddle)。
但是,我想实现这一点,以便在第一次打开页面时折叠树的所有顶级目录(在顶部示例中是父文件夹),但是单击其中一个顶级目录将完全打开向上所有子分支。现在它只会向下打开一层。如果您能帮助我完成这项工作,我将不胜感激。
树的当前 Javascript 如下。
$(function () {
$('.tree li:has(ul)').addClass('parent_li').find(' > span').attr('title', 'Collapse this branch');
$('.tree li ul > li').hide();
$('.tree li.parent_li > span').on('click', function (e) {
var children = $(this).parent('li.parent_li').find(' > ul > li');
if (children.is(":visible")) {
children.hide('fast');
$(this).attr('title', 'Expand this branch').find(' > i').addClass('icon-plus-sign').removeClass('icon-minus-sign');
} else {
children.show('fast');
$(this).attr('title', 'Collapse this branch').find(' > i').addClass('icon-minus-sign').removeClass('icon-plus-sign');
}
e.stopPropagation();
});
});
最佳答案
尝试改变
var children = $(this).parent('li.parent_li').find(' > ul > li');
到
var children = $(this).parent('li.parent_li').find('li');
关于javascript - 仅折叠 CSS 树中的顶层,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38927449/