这是一个可能的解决方案:
HTML:
<a href="#">Parent item name</a>
<ul class="0" id="level_0_count_0" data-level='0'>
<li>
<a href="#">Child item name</a>
<ul class="1" id="level_1_count_0" data-level='1'>
<li><a href="#">ul 0 2nd level item 1pppppppp</a></li>
<li><a href="#">ul 0 2nd level item 2pppppppp</a></li>
<li><a href="#">ul 0 2nd level item 3pppppppp</a></li>
<li>
<a href="#">ul 0 2nd level item 4</a>
<ul class="2" id="level_2_count_0" data-level='1'>
<li><a href="#">ul 2 3nd level item 1cccccccc</a></li>
<li><a href="#">ul 2 3nd level item 2cccccccc</a></li>
<li><a href="#">ul 2 3nd level item 3cccccccc</a></li>
<li><a href="#">ul 2 3nd level item 4cccccccc</a></li>
</ul>
</li>
</ul>
</li>
<li>
<a href="#">Child item name</a>
<ul class="1" id="level_1_count_1" data-level='1'>
<li><a href="#">ul 1 2nd level item 1</a></li>
<li><a href="#">ul 1 2nd level item 2</a></li>
<li><a href="#">ul 1 2nd level item 3</a></li>
<li><a href="#">ul 1 2nd level item 4</a></li>
</ul>
</li>
JavaScript:
var $j = jQuery.noConflict();
$j(document).ready(function() {
var temp = new Array();
find_ul(0, temp);
});
function find_ul(parent_class, new_object_, level_count){
if( $j('.' + parent_class + ' > li > ul').length == 0){
return new_object_;
}else{
var current_objs = $j('.' + parent_class + ' > li > ul');
var i = 0;
parent_class++;
$j.each(current_objs, function() {
var current_ul_id = '';
var object_lis = $j('ul#level_' + parent_class + '_count_' + i + ' > li');
var b = 0;
var tempora = new Array();
var current_ul_id = $j(this).attr('id');
alert(current_ul_id);
$j.each(object_lis, function() {
tempora[b] = $j(this).html();
b++;
alert($j(this).html());
});
new_object_[i] = {ul_id: current_ul_id, lis:tempora};
i++;
});
}
find_ul(parent_class, new_object_);
}
您可以在以下位置查看/测试它:http:
//jsfiddle.net/BsDpY/4/
这将为 ul/li 的每一层生成一个包含级别的数组
因此它将首先通过第一级(父母)然后通过其子 UL ..
希望有帮助。