2016-11-30 8 views
0

私は動的に生成されたツリービューを持っていますULとLI要素を動的にループする方法は?

アイデアはいくつかの値を取得するためだけにループすることですが、どの要素がどのように多くの子を持つかはわかりません。

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

</ul> 
+1

再帰関数を使用する必要があります。 –

+1

[jQueryの可能な複製]特定のDIV内のUL内のすべてのLI要素を検索して一覧表示する(http://stackoverflow.com/questions/7101945/jquery-find-and-list-all-li-elements-within-a -ul-within-a-specific-div) –

+0

値に属性を使用するとはるかに簡単です – monssef

答えて

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

それが葉を見つけ、そのブランチの全ての葉が処理された後、あなたが最初にして、葉の上に枝を動作させることができますまで、このコードは、再帰的にあなたのツリーを通過します。

関連する問題