我试图翻译像
这样的嵌套列表<ul>
<li>Coffee</li>
<li>Tea
<ul>
<li>Black tea</li>
<li>Green tea</li>
</ul>
</li>
<li>Milk</li>
</ul>
到
<div>
<div class="list-depth-1">Coffee</div>
<div class="list-depth-1">Tea</div>
<div class="list-depth-2">Black tea</div>
<div class="list-depth-2">Green tea</div>
<div class="list-depth-1">Milk</div>
</div>
你能帮帮我吗?这个订单总是一样的(从上到下)?
答案 0 :(得分:0)
this怎么样?
var parent = $("<div></div>");
$('#top li').each(function(pos,elem){
var child = $("<div></div>").addClass("list-depth-" + $(elem).parents('ul').length).text(elem.childNodes[0].nodeValue);
parent.append(child);
});
$('body').append(parent);