Jquery - 遍历所有 xml 标签
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1735026/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Jquery - iterate through all xml tags
提问by dedoz
how to loop over all tags in a xml
如何遍历xml中的所有标签
i have a php that generates xmls like the next one
我有一个生成 xmls 的 php,就像下一个
<register>
<name>peter</name>
<age>12</age>
</register>
<register>
<name>mary</name>
<age>20</age>
</register>
so i receive this xml (this works fine)
所以我收到了这个 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?
});
}});
采纳答案by jtbandes
You'll want to look at the children()
function, among others. See the jQuery traversal documentationfor more info on the functions.
您将需要查看该children()
函数等。有关函数的更多信息,请参阅jQuery 遍历文档。
回答by Ima
This link provides a good example for use of iterating through xml
此链接提供了一个使用 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
http://anasthecoder.blogspot.in/2012/02/looping-through-xml-with-jquery.html