jQuery XML解析如何获取元素属性 [英] jQuery XML parsing how to get element attribute

查看:68
本文介绍了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获取CSS,在onsuccess函数中,我的代码如下:

I am fetching css from ajax and in onsuccess function my code is as follow:

$(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')时,它没有给我任何东西 有人能弄清楚我在做什么错吗? 谢谢

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

推荐答案

您的XML使用名称空间.

Your XML uses a namespace.

请参阅有关名称空间的类似堆栈溢出问题.您必须逃脱冒号:

See this similar Stack Overflow question regarding namespaces. You have to escape the colon:

使用命名空间的jQuery XML解析

这篇关于jQuery XML解析如何获取元素属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆