我有一个这样的代码下拉列表:
<select id="Folder" name="Folder">
<option value="0">Top Folder</option>
<option value="11">2nd Folder 01</option>
<option value="17">---3rd Folder 01-01</option>
<option value="18">---3rd Folder 01-02</option>
<option value="383">------4th Folder 01-02-01</option>
<option value="384">---------5th Folder 01-02-01-01</option>
<option value="385">---------5th Folder 01-02-01-02</option>
<option value="386">---------5th Folder 01-02-01-03</option>
<option value="387">---------5th Folder 01-02-01-04</option>
<option value="388">---------5th Folder 01-02-01-05</option>
<option value="19">---3rd Folder 03</option>
<option value="20">---3rd Folder 04</option>
<option value="22">---3rd Folder 05</option>
<option value="130">------5th Folder 01-02-05-01</option>
<option value="131">------5th Folder 01-02-05-02</option>
<option value="132">------5th Folder 01-02-05-03</option>
<option value="133">------5th Folder 01-02-05-04</option>
<option value="134">------5th Folder 01-02-05-05</option>
<option value="398">------5th Folder 01-02-05-06</option>
<option value="158">2nd Folder 02</option>
<option value="257">---3rd Folder 02-01</option>
<option value="258">---3rd Folder 02-02</option>
<option value="259">---3rd Folder 02-03</option>
<option value="16">2nd Folder 03</option>
<option value="382">---3rd Folder 03-01</option>
<option value="51">---3rd Folder 03-02</option>
<option value="113">------4th Folder 03-02-01</option>
<option value="125">------4th Folder 03-02-02</option>
</select>
我用 jquery 将它们转换成一个列表:
<script type="text/javascript">//<![CDATA[
$(window).load(function(){
$(function() {
var $yearFilter = $("#Folder");
$yearFilter.find("option").map(function() {
var $this = $(this);
return $("<li>").attr("value",$this.attr("value")).text($this.text()).get();
}).appendTo($("<ul>").attr({
id: $yearFilter.attr("id"),
name: $yearFilter.attr("name")
})).parent().replaceAll($yearFilter);
});
});//]]>
</script>
这给了我这个:
<ul id="Folder" name="Folder">
<li value="0">Top Folder</li>
<li value="11">2nd Folder 01</li>
<li value="17">---3rd Folder 01-01</li>
<li value="18">---3rd Folder 01-02</li>
<li value="383">------4th Folder 01-02-01</li>
<li value="384">---------5th Folder 01-02-01-01</li>
<li value="385">---------5th Folder 01-02-01-02</li>
<li value="386">---------5th Folder 01-02-01-03</li>
<li value="387">---------5th Folder 01-02-01-04</li>
<li value="388">---------5th Folder 01-02-01-05</li>
<li value="19">---3rd Folder 03</li>
<li value="20">---3rd Folder 04</li>
<li value="22">---3rd Folder 05</li>
<li value="130">------5th Folder 01-02-05-01</li>
<li value="131">------5th Folder 01-02-05-02</li>
<li value="132">------5th Folder 01-02-05-03</li>
<li value="133">------5th Folder 01-02-05-04</li>
<li value="134">------5th Folder 01-02-05-05</li>
<li value="398">------5th Folder 01-02-05-06</li>
<li value="158">2nd Folder 02</li>
<li value="257">---3rd Folder 02-01</li>
<li value="258">---3rd Folder 02-02</li>
<li value="259">---3rd Folder 02-03</li>
<li value="16">2nd Folder 03</li>
<li value="382">---3rd Folder 03-01</li>
<li value="51">---3rd Folder 03-02</li>
<li value="113">------4th Folder 03-02-01</li>
<li value="125">------4th Folder 03-02-02</li>
</ul>
但是,我想要实现的是:
<ul id="Folder">
<li>Top Folder</li>
<li>2nd Folder 01
<ul class="something01">
<li>3rd Folder 01-01</li>
<li>3rd Folder 01-02
<ul>
<li>4th Folder 01-02-01
<ul class="something01-02-01">
<li>5th Folder 01-02-01-01</li>
<li>5th Folder 01-02-01-02</li>
<li>5th Folder 01-02-01-03</li>
<li>5th Folder 01-02-01-04</li>
<li>5th Folder 01-02-01-05</li>
</ul>
</li>
</ul>
</li>
<li>3rd Folder 03</li>
<li>3rd Folder 04</li>
<li>3rd Folder 05
<ul class="something05">
<li>5th Folder 01-02-05-01</li>
<li>5th Folder 01-02-05-02</li>
<li>5th Folder 01-02-05-03</li>
<li>5th Folder 01-02-05-04</li>
<li>5th Folder 01-02-05-05</li>
<li>5th Folder 01-02-05-06</li>
</ul>
</li>
</ul>
</li>
<li>2nd Folder 02
<ul class="something02">
<li>3rd Folder 02-01</li>
<li>3rd Folder 02-02</li>
<li>3rd Folder 02-03</li>
</ul>
</li>
<li>2nd Folder 03
<ul class="something03">
<li>3rd Folder 03-01</li>
<li>3rd Folder 03-02
<ul>
<li>4th Folder 03-02-01</li>
<li>4th Folder 03-02-02</li>
</ul>
</li>
</ul>
</li>
</ul>
所以我想知道是否有一种方法可以根据破折号“-”将所有第二、第三等级别分组到一个 ul 组中。还是我必须给每个 li 一个班级然后从那里分组?