【问题标题】:HOW TO LOOP dynamically into UL and LI elements?如何动态循环到 UL 和 LI 元素中?
【发布时间】:2017-04-14 04:26:07
【问题描述】:

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

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

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

</ul>

【问题讨论】:

标签: javascript jquery html css


【解决方案1】:
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'));

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2013-05-27
    • 2011-08-22
    • 1970-01-01
    • 2012-10-03
    • 1970-01-01
    • 2020-11-25
    • 2018-12-20
    • 1970-01-01
    相关资源
    最近更新 更多