如何使用jquery在列表视图中显示父项的子项?

时间:2013-06-26 06:02:56

标签: jquery xml

我正在尝试动态地使用jquery在listview中显示xml文件父级的子标签。

$(xml).find('section[order="' + order + '"] content').each(function () {
     var content = $(this).text();
var section = $(this).find('section').attr("name");
 $("#section_list").append(section+'<li><a href="#" class="style1"><h2>' + content + ' </h2></a></li> ');
$("#section_list").listview("refresh");
});

这是我的代码我在列表视图中获取子标记,但是每个孩子都会获得父标记。

XML文件:

<?xml version="1.0" encoding="UTF-8" standalone="no"?>
  <sections>
    <section count="6" name="Alphabets" order="1">
      <content order="1">A</content>
      <content order="2">B</content>
      <content order="3">C</content>
      <content order="4">D</content>
      <content order="5">E</content>
      <content order="6">F</content>
    </section>
    <section count="4" name="Numbers" order="2">
      <content order="7">1</content>
      <content order="8">2</content>
      <content order="9">3</content>
      <content order="10">4</content>
    </section>
</sections>

欲望输出是

字母

A

C

d

电子

˚F

编号

1

2

3

4

“ABCDEF”和“1234”应显示在列表视图中,父标记应如上所示。

先谢谢。

1 个答案:

答案 0 :(得分:0)

    for (var order = 1; order < 3; order++) {
        $('xml').find('section[order="' + order + '"]').each(function () {
            //iterate by section
            var sectionName = $(this).attr("name");
            //Add Section Name to the list
            $("#section_list").append(sectionName);
            $(this).children().each(function () {
                //iterate by section/content
                //for each content tag under section add a li element
                $("#section_list").append('<li><a href="#" class="style1"><h2>' + $(this).text() + ' </h2></a></li> ');
            });
            //$("#section_list").listview("refresh");
        });
    }

试试这个fiddle