Javascript 树中节点的非标准检查
我需要一些建议,当我单击父节点时,我如何实现一个函数来检查下一个到最后一个节点,该节点也是此级别上的最后一个子节点及其子节点。更具体地说,我将尝试用简单的例子更好地解释它。当我们有一棵这样的树:Javascript 树中节点的非标准检查,javascript,treeview,Javascript,Treeview,我需要一些建议,当我单击父节点时,我如何实现一个函数来检查下一个到最后一个节点,该节点也是此级别上的最后一个子节点及其子节点。更具体地说,我将尝试用简单的例子更好地解释它。当我们有一棵这样的树: Node1 Node9 Node11 Node15, Node14 (Node4, Node5 - Node15's children), (Node2, Node3 - Node14's children) 当我单击例如Node9时,应同时选中Node1、Node15、Node4和Node5元素 我
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');
}
}
});
}
谢谢@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 last child的子节点的最低级别的叶,但也检查其余n-1节点的叶。到目前为止,我不知道为什么我评论的两个条件中的任何一个都不起作用。类似$this.parent.find>*