0
我試圖在JavaScript中的遞歸函數應該基本上給我HTML像下面使用JavaScript遞歸函數不工作
<li><a class="expand">+</a> <span class="treeNodeInner"><a id="7471">Ringtones</a>
<ul>
<li><span class="treeNodeInner"><a id="7995">Top Tones</a></span></li>
<li><span class="treeNodeInner"><a id="8642">Country</a></span></li>
<li><span class="treeNodeInner"><a id="8640">Rock</a></span></li>
</ul>
</span></li>
我使用下面的功能,使使用javascript上面的HTML創建HTML。 它基本上是一個樹視圖,我們將有節點和子節點的東西。
我奮力的實現通過以下功能上面的HTML結構,請與下面的功能建議修改
function GenerateNodes(categItem) {
var parentNode = "<li>"; //parent li
if (categItem.SubCategory != null && categItem.SubCategory != undefined && categItem.SubCategory.Count > 0) {
parentNode = "<li><a class='expand'>+</a>";
}
parentNode += "<input type='radio' name='category' /><span class='treeNodeInner'><a id='" + categItem.ID + "'>" + categItem.name + "</a> "; //need to close the span
if (categItem.SubCategory != null && categItem.SubCategory != undefined && categItem.SubCategory.Count > 0) {
var subNode = "<span><ul>";
for (var index = 0; index < categItem.SubCategory.Count; index++) {
subNode += GenerateNodes(categItem.SubCategory[index]);
subNode += "</ul></span>"
}
parentNode += subNode
}
parentNode += "</span></li>"
}
感謝
你還是不在你的代碼中使用jQuery?我沒有在您的示例中看到jQuery功能的單一引用。 – 2011-05-09 09:40:24
好吧,我現在沒有使用任何jquery,但如果有人可以告訴我,我會用它 – Amit 2011-05-09 09:41:55
而不是'categItem.SubCategory!= null && categItem.SubCategory!= undefined'你可以只有'catagItem.SubCategory'如果它是'null'或'undefined',它將評估爲'false'。 – geekchic 2011-05-09 09:42:22