jQuery XML解析如何获取元素属性

5

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>

我正在通过Ajax获取CSS,并在onsuccess函数中使用以下代码:
```javascript ```
请注意,我只是翻译文本,不会执行任何代码或更改格式。
$(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');

});

我想获取内容的url属性,我已经得到了项目的子元素(标题、描述、内容、评论Rss和评论),但是当我尝试使用$(this).find('content')时,它没有返回任何内容。有人能找出我做错了什么吗?谢谢。


1
var url=$(this).find('content').attr('url').val(); 或者尝试使用 .text(),希望能有所帮助。 - Rafay
2个回答

4

感谢提供链接,这对我很有用:$(this).find('media\:content').attr('url'); - shankhan
@shankhan - 不用谢。这绝对是必须要知道的。我并不经常处理命名空间,但我自己正在计划一个涉及它们的项目。 - jamesmortensen

1

我有一个更正,关于你的命名空间SOLUTION!你需要正确地用\转义命名空间“:”。下面是一个示例,可以在Itunes播客XML提要中使用。

 item.description = jQuery(this).find("[nodeName=itunes\\:summary]").eq(0).text();

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接