【问题标题】:jQuery XML traversal of child using .each()使用 .each() 对子项进行 jQuery XML 遍历
【发布时间】:2017-10-24 11:40:47
【问题描述】:

我正在尝试解析 xml 以在 div 中输出,但我遇到了孩子的问题。

我做错了什么?我必须在不修改 xml 的情况下解析它。

预期输出:

会员 1

爱好:
-爱好 1
-爱好 1
-爱好 1

会员 2

爱好:
-爱好 2
-爱好 2
-爱好 2

实际输出:

会员 1

爱好:
-爱好 1
-爱好 1
-爱好 1
-爱好 2
-爱好 2
-爱好 2

会员 2

爱好:
-爱好 1
-爱好 1
-爱好 1
-爱好 2
-爱好 2
-爱好 2

XML:

 <members>
  <person>
    <name>Member 1</name>
    <hobbies>
     <hobby>Hobby 1</hobby>
     <hobby>Hobby 1</hobby>
     <hobby>Hobby 1</hobby>
    </hobbies>
   </person>
   <person>
   <name>Member 2</name>
   <hobbies>
     <hobby>Hobby 2</hobby>
     <hobby>Hobby 2</hobby>
     <hobby>Hobby 2</hobby>
   </hobbies>
</person>

代码:

<script>
  $(document).ready(function(){
    $.ajax({
    type: "GET",
    url: "test.xml",
    dataType: "xml",
    success: function(xml) {
      $(xml).find('person').each(function(){
        $('.members').append(
          '<div class="person">' +
          '<h2>' + $(this).find('name').text() + '</h2>' +
          '<br />' +
          '<h3>Hobbies:</h3>' +
          '<br />' +
          '<ul class="hobbies">' +
          '</ul>' +
          '</div>' +
          '<br />' +
          '<br />'
           )
        });

        $(xml).find('hobbies').children().each(function () {
          $('<li>' + $(this).text() + '</li>').appendTo('.hobbies')
        });
       }
      });
    });
 </script>

【问题讨论】:

标签: jquery xml


【解决方案1】:

你只需要找到它当前的孩子。

$(document).ready(function(){
    $.ajax({
    type: "GET",
    url: "test.xml",
    dataType: "xml",
    success: function(xml) {
      $(xml).find('person').each(function(){
        $('.members').append(
          '<div class="person">' +
          '<h2>' + $(this).find('name').text() + '</h2>' +
          '<br />' +
          '<h3>Hobbies:</h3>' +
          '<br />' +
          '<ul class="hobbies">' +
          '</ul>' +
          '</div>' +
          '<br />' +
          '<br />'
           )

           $(this).find('hobbies').children().each(function () {
                $('<li>' + $(this).text() + '</li>').appendTo('.hobbies')
            });
        });
       }
      });
    });

【讨论】:

  • 是否有关于如何做到这一点的文档?
【解决方案2】:

您寻找爱好的内部循环是找到您的 xml 的两个 &lt;hobbies&gt; 节点。你需要更具体。您可以通过在 .each 循环中引用您正在访问的元素来做到这一点:

<script>
  $(document).ready(function(){
    $.ajax({
    type: "GET",
    url: "test.xml",
    dataType: "xml",
    success: function(xml) {
      // In your callback, name the reference to index and element
      $(xml).find('person').each(function(num, elem){
        $('.members').append(
          '<div class="person">' +
          '<h2>' + $(this).find('name').text() + '</h2>' +
          '<br />' +
          '<h3>Hobbies:</h3>' +
          '<br />' +
          '<ul class="hobbies">' +
          '</ul>' +
          '</div>' +
          '<br />' +
          '<br />'
        );

        // only find the hobbies node of this element, not whole xml
        $(elem).find('hobbies').children().each(function () {
          $('<li>' + $(this).text() + '</li>').appendTo('.hobbies')
        });
      });


    }
    });
  });
</script>

【讨论】:

  • 当我尝试得到 elem undefined 时?
  • 我犯了一个错误 - $(elem) 部分应该在 .each 回调中。我已经更正了答案。
猜你喜欢
  • 2010-12-26
  • 2013-11-01
  • 2012-08-11
  • 1970-01-01
  • 1970-01-01
  • 2017-01-06
  • 2013-10-15
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多