![](/img/trans.png)
[英]how to dynamically create ul and li elements by for loop in javascript
[英]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.