【问题标题】:Jquery - iterate through all xml tagsJquery - 遍历所有 xml 标签
【发布时间】:2009-11-14 17:52:44
【问题描述】:

如何遍历 xml 中的所有标签

我有一个 php 可以像下一个一样生成 xmls

<register>
  <name>peter</name>
  <age>12</age>
</register> 
<register>
  <name>mary</name>
  <age>20</age>
</register> 

所以我收到了这个 xml(这工作正常)

$.ajax({success: function(xml) {  

   $(xml).find('register').each (function() 
   { 
     alert($(this).find('name').text()) // works fine, shows peter then mary on the next loop of "each"



     // But if i dont know the tag names (name,age) for each register ?     

     // Something like

      $(this).nodes().each .... // 
 alert($(this).tagName);  // i wanna show "name" & "age", how can i get the tag names inside each register in my xml sample tree?

   });    

}});

【问题讨论】:

    标签: jquery


    【解决方案1】:

    此链接提供了一个很好的使用 xml 迭代的示例

    xml.find('result').find('permissionDetails').each(function(){
        $(this).children().each(function(){
            var tagName=this.tagName;
            var val=$(this).text();
            if(val==1){
                $('input:checkbox[name='+tagName+']').attr('checked',true);
            }
            else if(val==0){
                $('input:checkbox[name='+tagName+']').removeAttr('checked');
            }
        })
    });
    

    http://anasthecoder.blogspot.in/2012/02/looping-through-xml-with-jquery.html

    【讨论】:

      【解决方案2】:

      您需要查看children() 函数等。有关函数的更多信息,请参阅jQuery traversal documentation

      【讨论】:

      • 感谢您的快速 asnwer 但我仍然无法做到 =((我很伤心......)在 jquery 帮助中说 var $kids = $(e.target).children(); 但是仍然 .. 那 prolly 给了我一些带有孩子的对象/数组,但是我如何获得每个孩子的标签名?这不起作用 $(this).children().each(function() {alert($(this). tagName);}); // 这也不起作用,显示所有方法/att var childs = $(this).children(); for(key in childs) alert(key);
      • 终于.. var $childs = $(this).children();变种长度 = $childs.length; while(length--) alert($childs[length].tagName);非常感谢你
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2011-07-21
      • 2017-10-12
      • 2016-03-08
      • 1970-01-01
      • 1970-01-01
      • 2018-09-16
      • 1970-01-01
      相关资源
      最近更新 更多