开发者

append <li> to <ul> using JQuery

开发者 https://www.devze.com 2023-04-04 09:32 出处:网络
i am trying to create a nested ul by reading values from XML file. I am able to create first level ul but not able to add li elements under that. Firebug is not displaying any error. Please help code

i am trying to create a nested ul by reading values from XML file. I am able to create first level ul but not able to add li elements under that. Firebug is not displaying any error. Please help code is given below;

$(function(){
    $.get("../XML/test.xml",processResult); 
});

function processResult(data){
    $(data).find("Category").each(showCategory);
}

function showCategory(){
    var catName = $(this).attr("Title");
    $("#menuList").app开发者_JAVA技巧end("<ul>" + catName + "</ul>");
    $(this).find("Function").each(showFunction);
}

function showFunction(){
    var funcName = $(this).attr("Title");
    $(this.parentNode).append("<li>" + funcName + "</li>");
}


You are putting text directly inside a <ul> element, and using the wrong this variable. Try something like this:

$(function(){
    $.get("../XML/test.xml",processResult); 
});

function processResult(data){
    $(data).find("Category").each(showCategory);
}

function showCategory(){
    var catName = $(this).attr("Title");
    var parent = $("<ul></ul>");
    $(this).find("Function").each(function(){
      var funcName = $(this).attr("Title");
      parent.append("<li>" + funcName + "</li>");
    });
    $("#menuList").append(catName).append(parent);
}

Let me know if it works. If you have sample data available it will be easier to verify that it functions correctly.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号