我的菜單項的數組,每個都包含名稱和URL這樣的URL路徑結構嵌套UL菜單:創建基於菜單項
var menuItems = [
{
name : "Store",
url : "/store"
},
{
name : "Travel",
url : "/store/travel"
},
{
name : "Gardening",
url : "/store/gardening"
},
{
name : "Healthy Eating",
url : "/store/healthy-eating"
},
{
name : "Cook Books",
url : "/store/healthy-eating/cook-books"
},
{
name : "Single Meal Gifts",
url : "/store/healthy-eating/single-meal-gifts"
},
{
name : "Outdoor Recreation",
url : "/store/outdoor-recreation"
},
{
name : "Hiking",
url : "/store/outdoor-recreation/hiking"
},
{
name : "Snowshoeing",
url : "/store/outdoor-recreation/hiking/snowshoeing"
},
{
name : "Skiing",
url : "/store/outdoor-recreation/skiing"
},
{
name : "Physical Fitness",
url : "/store/physical-fitness"
},
{
name : "Provident Living",
url : "/store/provident-living"
}
]
我一直沒有成功試圖渲染這是一個嵌套的UL結構後面的URL路徑結構,像這樣一個無序列表:
<ul>
<li><a href="/store">Store</a>
<ul>
<li><a href="/store/travel">Travel</a></li>
<li><a href="/store/gardening">Gardening</a></li>
<li><a href="/store/healthy-eating">Healthy Eating</a>
<ul>
<li><a href="/store/healthy-eating/cook-books">Cook Books</a></li>
<li><a href="/store/healthy-eating/single-meal-gifts">Single Meal Gifts</a></li>
</ul>
</li>
<li><a href="/store/outdoor-recreation">Outdoor Recreation</a>
<ul>
<li><a href="/store/outdoor-recreation/hiking">Hiking</a>
<ul>
<li><a href="/store/outdoor-recreation/hiking/snowshoeing">Snowshoeing</a></li>
</ul>
</li>
<li><a href="/store/outdoor-recreation/skiing">Skiing</a></li>
</ul>
</li>
<li><a href="/store/physical-fitness">Physical Fitness</a></li>
<li><a href="/store/provident-living">Provident Living</a></li>
</ul>
</li>
</ul>
所有我見過有一個數據結構,反映了親子關係(例如XML或開始實例JSON),但我有一個非常困難的時間拉出這個網址和使用它來呈現新的結構。
如果有人可以請指導我正確的方向使用jQuery如何做到這一點,我真的很感激它。我意識到我可能需要使用一些遞歸函數或jQuery模板,但這些東西對我來說還是有點新的。
謝謝
這工作得很好,但一個「例外」:如果有一個子節點沒有定義父節點就會像父母/ mainnode添加。我不知道這是否是一種想要的副作用,但它可能是合理的。 – buschtoens
對不起,但這不起作用,我試了幾次你的代碼。它適用於第一個節點,但當下一個兄弟節點中有子節點時,下一個兄弟節點的子節點將被添加到第一個節點的子節點列表中。 –
@Davide,如何使用成形的HTML數據來渲染樹?再次,遞歸函數 – 2017-08-23 08:04:21