仅折叠 CSS 树中的顶层

Collapsing only the top level in CSS tree

在我的模板中,我使用了与 this question. The answer to that question opens the page up with the tree entirely collapsed (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');