jQuery XML 解析如何获取元素属性
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4825920/
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 XML parsing how to get element attribute
提问by shankhan
XML:
XML:
<item>
<title>Some title</title>
<description>
<![CDATA[
Some description text Some description text Some description text Some description text Some description text Some description text
Some description text Some description text Some description text Some description text Some description text Some description text
Some description text Some description text Some description text Some description text Some description text Some description text
Some description text Some description text Some description text Some description text Some description text Some description text
]]>
</description>
<media:content isDefault="true" medium="image" url="http://sampledomain.com/image.jpg">
<media:title>Image title</media:title>
<media:description>Image description</media:description>
<media:thumbnail url="http://sampledomain.com/image.jpg" width="160" height="120" />
</media:content>
<wfw:commentRss>http://sampledomain.com/comment/feed/</wfw:commentRss>
<slash:comments>0</slash:comments>
</item>
I am fetching css from ajax and in onsuccess function my code is as follow:
我正在从 ajax 获取 css,在 onsuccess 函数中,我的代码如下:
$(xml).find('item').each(function() {
var title = $(this).find('title').eq(0).text();
var url = $(this).find('content').attr('url');
// Printing url as undefined
alert(url);
console.log($(this).find('content');
});
What I want to get content url attribute I am getting item children (title, description, content, commentRss and comments) but when I try to get $(this).find('content') It is not giving me anything Can anyone figure it out what am I doing wrong? Thanks
我想要获取内容 url 属性的内容我正在获取项目子项(标题、描述、内容、commentRss 和评论)但是当我尝试获取 $(this).find('content') 时它没有给我任何东西任何人都可以想象搞清楚我做错了什么?谢谢
采纳答案by jmort253
Your XML uses a namespace.
您的 XML 使用命名空间。
See this similar Stack Overflow question regarding namespaces. You have to escape the colon:
请参阅有关名称空间的类似 Stack Overflow 问题。你必须逃避冒号:
回答by John Drefahl
Got a correction for you.. and your Namespace SOLUTION! You need to escape the namespace ":" properly with \. I have show the example below which works with the Itunes podcast XML feeds.
有一个更正给你..和你的命名空间解决方案!您需要使用 \ 正确转义命名空间“:”。我已经展示了下面的示例,该示例适用于 Itunes 播客 XML 提要。
item.description = jQuery(this).find("[nodeName=itunes\:summary]").eq(0).text();