2015-01-10 57 views
0

我需要一些建议我怎么才能实现一个函数来检查最后一个子节点的最后一个节点的下一个节点,当我单击父节点时,它也是子节点。更具体地说,我会尝试用简单的例子更好地解释它。当我们有这样一棵树时:树中节点的非标准检查

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'); 
      } 
     } 
    }); 
} 

THX提前任何建议,就如何处理它。

+0

类似于$(this).parent()。find(“> *”) – dandavis

回答

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)个节点。到目前为止,我不知道为什么两个人评论我的条件不起作用。