簡體   English   中英

樹中節點的非標准檢查

[英]nonstandard check of nodes in tree

我需要一些建議,當我單擊父節點時,如何實現一個函數來檢查該節點上的最后一個子節點,即該級別的最后一個子節點。 更具體地說,我將嘗試通過簡單的示例對其進行更好的解釋。 當我們有一棵這樣的樹時:

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)個最后一個子節點的子節點,但是葉子也檢查其余(n-1)個節點。 到目前為止,我不知道為什么我所評論的兩個條件中的任何一個都不起作用。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM