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

标签 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 函数中我的代码如下:

$(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 使用命名空间。

请参阅关于 namespace 的类似 Stack Overflow 问题。你必须避开冒号:

jQuery XML parsing with namespaces

关于jQuery XML解析如何获取元素属性,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4825920/

相关文章:

c# - 如何从简单的 xml 文件创建列表 <string>?

jquery - Coldfusion 和 jquery ajax

sql - 遍历 SQL Server 中的 XML 列

java - JAXB 将 XML 片段映射为另一个 XML 文档中的片段

java - 从生成的源创建 XML

Android Studio 更改 xml 代码样式问题

javascript - Twitter Bootstrap TypeAhead 与具有自动完成功能的下拉列表/选择标签一样工作

Javascript智能过滤对象数组

javascript - 搜索并用 alt 值替换 td 内的 img 标签

javascript - 使用 JQuery Ajax 请求的 ASP.NET Web 表单方法失败