使用JQuery将附加到

时间:2022-04-16 20:34:16

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;

我试图通过从XML文件中读取值来创建嵌套的ul。我能够创建第一级ul但不能在其下添加li元素。 Firebug没有显示任何错误。请帮助代码如下;

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

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

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

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

1 个解决方案

#1


1  

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.

如果有效,请告诉我。如果您有可用的样本数据,则可以更轻松地验证其是否正常运行。

#1


1  

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.

如果有效,请告诉我。如果您有可用的样本数据,则可以更轻松地验证其是否正常运行。