简体   繁体   English

如何动态地加入UL和LI元素?

[英]HOW TO LOOP dynamically into UL and LI elements?

I have a tree view generated dynamically 我有一个动态生成的树状视图

the idea is to loop into them just to get the some values, but we don't know how many childs every element has. 想法是循环进入它们只是为了获得一些值,但我们不知道每个元素有多少个孩子。

<ul>
   <li>something here</li>
   <li>something here
        <ul>
           <li></li>
           <li></li>
            ...more and more childs
        </ul>
   </li>

</ul>
function getNode($node) {
  var $children = $node.children();
  if ($children.length) {
    $children.each(function() {
        getNode($(this));
      })
      //Do something with the branch
  } else {
    //Do something with the leaf
  }
}

getNode($('#your_tree_top_node'));

This code will recursively go through your tree until it finds the leaves and allows you to act on the leaves first and then the branches after all the leaves of that branch has been processed. 此代码将递归遍历您的树,直到找到叶子,并允许您先对叶子执行操作,然后在处理完该分支的所有叶子之后再对分支进行操作。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM