树中节点的非标准检查

时间:2015-01-10 01:20:37

标签: javascript treeview

我需要一些建议如何实现一个函数来检查下一个节点的最后一个节点,当我点击父节点时,它也是这个级别的最后一个子节点。更具体地说,我将尝试在简单的例子中更好地解释它。当我们有这样的树时:

Node1
Node9
Node11
Node15, Node14
(Node4, Node5 - Node15's children), (Node2, Node3 - Node14's children)

点击例如Node9,它应该检查Node1,Node15,Node4和Node5元素。

我有一个函数检查给定父节点的每个子节点,我想使用它,但我发现识别倒数第二个节点有些困难。是否有任何简单的方法来检查节点是否是给定路径的最后一个节点?

到目前为止,我有类似的东西:

function checkNonstandard(click, check) {

if(click.siblings().length>0) {
    click.siblings().each(function() {
        if($(this).is('ul') || $(this).is("li")) {
               // if($(this).children().is(":last-child")){
              // if($(this).children().length == 0){
                     //   $(this).removeAttr('checked');
//here I wanted with no luck to put a condition to check only the next to last node with children
                }
                else{
                     checkStandard($(this).children(), check);

//this is a function to check every node for given parent and its ancestors
                }
        } else if($(this).is("input")) {
            if(check==true) {
                $(this).attr('checked', true);
            } else {
                $(this).removeAttr('checked');
            }
        }
    });
}

提前知道如何处理它。

1 个答案:

答案 0 :(得分:0)

感谢@dandavis的回复,但毕竟我找不到它有用。我尝试了几个测试,几乎得到了它。我将代码更改为:

if(click.siblings().length>0) {
    click.siblings().each(function() {
        if($(this).is('ul') || $(this).is("li")) {
        if(($(this).children().children().children().length < 1) && !($(this).parent().is(":last-child"))){
                    $(this).children().removeAttr('checked');
                   // $(this).parent().not(":last-child").removeAttr('checked');
                  //  $(this).parent().removeAttr('checked');
                 }
                else{
                     checkNonstandard($(this).children(), check);
                }
        } else if($(this).is("input")) {
            if(check==true) {
                $(this).attr('checked', true);
            } else {
                $(this).removeAttr('checked');
            }
        }
    });

}

它几乎可以正常工作,因为只检查最后一个节点(n-1)最后一个子节点的子节点的最低级别叶子,但是还检查其余(n-1)个节点。到目前为止,我不知道为什么两个评论条件中的任何一个都不起作用。

相关问题