繁体   English   中英

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

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

我有一个动态生成的树状视图

想法是循环进入它们只是为了获得一些值,但我们不知道每个元素有多少个孩子。

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

此代码将递归遍历您的树,直到找到叶子,并允许您先对叶子执行操作,然后在处理完该分支的所有叶子之后再对分支进行操作。

暂无
暂无

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

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