2017-09-22 52 views
-1

在這個JS代碼中,我嘗試添加父級的所有嵌套元素到綠色元素,我嘗試添加類「TreeView-ActiveMoveDanger」。不幸的是,這隻會影響第一次嵌套,也就是說,如果進一步擴展樹,則不會通過按下'TreeView-ActiveToweDanger'類來添加以下元素。我的頭不明白,我不知道該怎麼想。Select color children TreeView

TreeView Example Image

function getTreeViews(el, move) { 
 
    $('.TreeView').each(function(i, e) { 
 
      if ($(this).hasClass('TreeView-Active')) { 
 
       $(this).removeClass('TreeView-Active'); 
 
      } 
 
      if ($(this).hasClass('TreeView-ActiveMoveDanger')) { 
 
       $(this).removeClass('TreeView-ActiveMoveDanger'); 
 
      } 
 
    }); 
 
    el.addClass('TreeView-Active'); 
 

 
    if (el.attr('data-id') != 0 && !el.hasClass('TreeView-ActiveMove')) { 
 
      var parent_id = el.parent('div').attr('id').split('TreeView-Move-Block-')[1]; 
 
      if ($('#move-row-' + parent_id).hasClass('TreeView-ActiveMove')) { 
 
       el.addClass('TreeView-ActiveMoveDanger'); 
 
      } else { 
 
       var ActiveMove = $('.TreeView-ActiveMove'); 
 
       if (ActiveMove.length) { 
 
        var ActiveMoveld = ActiveMove.attr('data-id'); 
 
        if (el.parents('#TreeView-Move-Block-' + ActiveMoveld).length) { 
 
         el.addClass('TreeView-ActiveMoveDanger'); 
 
        } 
 
       } 
 
      } 
 
    } 
 
}
.TreeView { 
 
\t padding: 5px 0 5px 15px; 
 
} 
 
.TreeView:hover { 
 
\t background: #e4ebfa; 
 
} 
 
.TreeView-Active { 
 
\t background: #e4ebfa; 
 
} 
 
.TreeView-ActiveMove { 
 
\t background: #ace6ac!important; 
 
} 
 
.TreeView-ActiveMoveDanger { 
 
\t background: #f58787!important; 
 
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script> 
 
<div class="col-xs-12" id="TreeView-Move-Block"> 
 
    <div class="TreeView clearfix pl-30 TreeView-ActiveMove" id="move-row-20" data-id="20" onclick="getTreeViews($(this), false)"> 
 
      Father 
 
    </div> 
 
    <div class="pl-30" id="TreeView-Move-Block-20"> 
 
     <div class="TreeView clearfix pl-30" id="move-row-17" data-id="17" onclick="getTreeViews($(this), false)"> 
 
       &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Child-1 
 
     </div> 
 
     <div class="pl-30" id="TreeView-Move-Block-17"> 
 
       <div class="TreeView clearfix pl-30" id="move-row-21" data-id="21" onclick="getTreeViews($(this), false)"> 
 
        &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Child-2 
 
       </div> 
 
     </div> 
 
    </div> 
 
    <div class="TreeView clearfix pl-30" id="move-row-22" data-id="22" onclick="getTreeViews($(this), false)"> 
 
      Sister 
 
    </div> 
 
</div>


更新| 我想出了一個解決方案,並不理想,但工作。更正了JS。事實證明,這並不完全。
更新| 再次糾正JS。由此我們必須繼續。
更新| 現在100%的作品。

+0

CSS的一部分?爲什麼不使用jssnippet? – Abra001

+0

完成。做了一個片段。一般來說,上面是截圖的鏈接。 – RAPOS

+0

你的css和html中的'pull-right'類,它不存在! – Abra001

回答

0

Вотмойполностьюрабочийкод

function getTreeViews(el, move) { 
    $('.TreeView').each(function(i, e) { 
      if ($(this).hasClass('TreeView-Active')) { 
       $(this).removeClass('TreeView-Active'); 
      } 
      if ($(this).hasClass('TreeView-ActiveMoveDanger')) { 
       $(this).removeClass('TreeView-ActiveMoveDanger'); 
      } 
    }); 
    el.addClass('TreeView-Active'); 

    if (el.attr('data-id') != 0 && !el.hasClass('TreeView-ActiveMove')) { 
      var parent_id = el.parent('div').attr('id').split('TreeView-Move-Block-')[1]; 
      if ($('#move-row-' + parent_id).hasClass('TreeView-ActiveMove')) { 
       el.addClass('TreeView-ActiveMoveDanger'); 
      } else { 
       var ActiveMove = $('.TreeView-ActiveMove'); 
       if (ActiveMove.length) { 
        var ActiveMoveld = ActiveMove.attr('data-id'); 
        if (el.parents('#TreeView-Move-Block-' + ActiveMoveld).length) { 
         el.addClass('TreeView-ActiveMoveDanger'); 
        } 
       } 
      } 
    } 
} 
+0

你能用英語重新表達嗎? – Abra001