我的Sqlite数据库数据:-

ID       ParentId       Item

1           0           Food

2           1           Indian

3           2           Full Meal

4           2           Meal

5           4           Roti

6           4           Dal

7           0           Chinese

8           7           Soup

9           8           Momo

10          9           Noodle


在Jquery中:

for ( var i = 0; i < results.rows.length; i++) {
    var food_Id = results.rows.item(i).ID ;
    var food_PId = results.rows.item(i).ParentId;
    var food_Item = results.rows.item(i).Item;
        if(food_PId == 0){
            sfoodID = food_Id;
            foodscripts +=  '<li><a href="#" class="d-main-menu">'
                            +'<img src="images/1.png" class="e-left" />'
                            +'<span class="d-menu-parent-text e-left">'+food_Item+'</span></a>'
        }else{
            foodscripts += '<ul>'
            for(var j = 0; j < results.rows.length; j++ ){
                var food_Id_s = results.rows.item(j).ID ;
                var food_PId_s = results.rows.item(j).ParentId ;
                var food_Item_s = results.rows.item(j).Item;
                    if(sfoodID == food_PId_s){
                        foodscripts += '<li><a href="">'+food_Item_s+'</a></li>'
                    }
            }
            foodscripts += '</ul>'
        }
    foodscripts +=  '</li>'
}


请帮助我,如果我现在不知道有多少级

我已经做到了一个高度

仅针对该级别内的两个或三个子级别执行操作

最佳答案

您好,我设置了Demo,其中将嵌套的ul与您提供的数据一起添加。
您需要进行一些更改,我才可以从虚拟源获取数据

var dataArray = new Array();
var data = new Array();data['ID'] = 1;data['ParentId'] = 0;data['Item'] = 'Food';dataArray.push(data);
var data = new Array();data['ID'] = 2;data['ParentId'] = 1;data['Item'] = 'Indian';dataArray.push(data);
var data = new Array();data['ID'] = 3;data['ParentId'] = 2;data['Item'] ='Full Meal';dataArray.push(data);
var data = new Array();data['ID'] = 4;data['ParentId'] = 2;data['Item'] = 'Meal';dataArray.push(data);
var data = new Array();data['ID'] = 5;data['ParentId'] = 4;data['Item'] = 'Roti';dataArray.push(data);
var data = new Array();data['ID'] = 6;data['ParentId'] = 4;data['Item'] ='Dal';dataArray.push(data);
var data = new Array();data['ID'] = 7;data['ParentId'] = 0;data['Item'] = 'Chinese';dataArray.push(data);
var data = new Array();data['ID'] = 8;data['ParentId'] = 7;data['Item'] = 'Soup';dataArray.push(data);
var data = new Array();data['ID'] = 9;data['ParentId'] = 8;data['Item'] ='Momo';dataArray.push(data);
var data = new Array();data['ID'] = 10;data['ParentId'] = 9;data['Item'] = 'Noodle';dataArray.push(data);


var food_Id = dataArray[i].ID;
var food_PId = dataArray[i].ParentId;
var food_Item = dataArray[i].Item;


代替这个

var food_Id = results.rows.item(i).ID ;
var food_PId = results.rows.item(i).ParentId;
var food_Item = results.rows.item(i).Item;

关于jquery - 如何附加嵌套的ul,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/22193594/

10-10 03:24