微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

jQuery 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>

我从ajax和onsuccess函数获取css,我的代码如下:

$(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属性我得到项目子项(标题,描述,内容,commentRSS评论)
但是当我试图获得$(this).find(‘content’)它并没有给我任何东西
任何人都可以弄清楚我做错了什么?
谢谢

解决方法

您的XML使用命名空间.

请参阅有关命名空间的类似Stack Overflow问题.你必须逃脱冒号:

jQuery XML parsing with namespaces

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐