Javascript 如何单独使用 jQuery 解析 xml 属性?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/5747335/
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
How to parse xml attributes with jQuery alone?
提问by Ryan
I'm already parsing xml successfully but i'm stuck at getting an attribute of childrens.
我已经成功解析了 xml,但我一直在获取 childs 的属性。
XML Example:
XML 示例:
<entries>
<entry>
<media:thumbnail url="blah" />
</entry>
</entries>
Javascript/ jQuery:
Javascript/jQuery:
$.get('data.xml', function(d){
$(d).find('entry').each(function(){
var $entry = $(this);
var pic = $entry.find('media:thumbnail').attr('url');
})
});
That javascript doesn't work for me to get an attribute. What's the problem?
该 javascript 不适合我获取属性。有什么问题?
回答by DarthJDG
Aah, namespaces are a different kind of animal, it wasn't in your original post. You have to escape the :
in your selector.
啊,命名空间是另一种动物,它不在您的原始帖子中。您必须:
在选择器中转义。
var pic = $entry.find('media\:thumbnail').attr('url');
See also jQuery XML parsing with namespaces
回答by mcgrailm
try this out
试试这个
$.ajax({
type: "GET",
url: 'data.xml,
dataType: "xml",
success: function(xml) {
$(xml).find('entry').each(function(){
var $entry = $(this);
var pic = $entry.find('picture').attr('url');
alert(pic);
})
},
error: function(xhr, status, error) {
if (xhr.status != 404) {alert(error);} else {alert("404 xml not found");}
}
})
回答by Devendra Madheshiya
$.get('data.xml', function(d) {
$(d).find('entry').each(function() {
var $entry = $(this);
var pic = $e`enter code here`ntry.find('media\:thumbnail, thumbnail').attr('url');
})
});