大佬教程收集整理的这篇文章主要介绍了使用jQuery读取RSS源 – 无法读取元素,大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。
下面是jquery代码:
function loadData(xml,ifid) { var htmlStr; var iframeToWrite = document.getElementById(ifid); htmlStr = "<html><body>" var items = $(xml).find("chAnnel item").each(function () { var $article = $(this); var title = $article.find("title").text(); var description = $article.find("description").text(); var link = $article.find("link").text(); var pubDate = $article.find("pubDate").text(); htmlStr += "<div class='RSSitem'>\n"; htmlStr += "\t<h3><a href='" + link + "' target='_blank' >" + title + "</a></h3>\n"; htmlStr += "\t<p>" + description + "</p>\n"; htmlStr += "\t<h6>" + pubDate + "</h6>\n"; htmlStr += "</div>\n" }); htmlStr += "</body></hmtl>"; iframeToWrite.contentDocument.write(htmlStr); }
下面是我从npr流编辑的示例xml:
<?xml version="1.0" encoding="UTF-8"?> <RSS xmlns:npr="http://www.nPR.org/RSS/" xmlns:nprml="http://api.nPR.org/nprml" xmlns:itunes="http://www.itunes.com/dtds/podcast-1.0.dtd" xmlns:content="http://purl.org/RSS/1.0/modules/content/" version="2.0"> <chAnnel> <title>News</title> <link>http://www.nPR.org/templates/story/story.PHP?storyId=1001&ft=1&f=1001</link> <description>NPR news,audio,and podcasts. Coverage of breaking stories,national and world news,politics,business,science,technology,and extended coverage of major national and world events.</description> <language>en</language> <copyright>Copyright 2012 NPR - For Personal Use Only</copyright> <generator>NPR API RSS Generator 0.94</generator> <lastBuildDate>Tue,28 Aug 2012 12:19:00 -0400</lastBuildDate> <image> <url>http://media.nPR.org/images/npr_news_123x20.gif</url> <title>News</title> <link>http://www.nPR.org/templates/story/story.PHP?storyId=1001&ft=1&f=1001</link> </image> <item> <title>Reports: Obama Administration Will Unveil New Fuel-Efficiency Standards</title> <description>The new rules will require U.s. cars to average 54.5 miles per gallon by 2025.</description> <pubDate>Tue,28 Aug 2012 12:19:00 -0400</pubDate> <link>http://www.nPR.org/blogs/thetwo-way/2012/08/28/160172356/reports-obama-administration-will-unveil-new-fuel-efficiency-standards?ft=1&f=1001</link> <guid>http://www.nPR.org/blogs/thetwo-way/2012/08/28/160172356/reports-obama-administration-will-unveil-new-fuel-efficiency-standards?ft=1&f=1001</guid> <content:encoded><![CDATA[<p>The new rules will require U.s. cars to average 54.5 miles per gallon by 2025.</p><p><a href="http://www.nPR.org/templates/email/emailAFriend.PHP?storyId=160172356">» E-Mail This</a> <a href="http://del.icio.us/post?url=http%3A%2F%2Fwww.nPR.org%2Ftemplates%2Fstory%2Fstory.PHP%3FstoryId%3D160172356">» Add to Del.icio.us</a></p>]]></content:encoded> </item> </chAnnel> </RSS>
如果你不使用parseXML并在它周围包装一个jQuery选择器,jQuery可能无法正确地插入所有节点.
在您的示例中,< link>标签未正确运行且< / link>已删除结束标记而未进行解析,因此无法正确查询链接文本.
您可以确认,如果您执行console.log($(this))并检查控制台输出中的内容.你可以看到< link>然后发短信但关闭< / link>不见了.
但是,在首先将xml解析为文档对象时,您现在可以在其周围包装jQuery选择器并可靠地访问所有节点.当然,假设初始XML字符串是有效的XMl.
DEMO – 在xml字符串上使用parseXml并在之后查询它
将此应用于您的代码看起来类似于:
htmlStr = "<html><body>"; // Parse the XML to a document object and then wrap it into a jQuery SELEctor var $xml = $($.parseXML(xml)); var items = $xml.find("chAnnel item").each(function() { var $article = $(this); //..... rest of your code as is
以上是大佬教程为你收集整理的使用jQuery读取RSS源 – 无法读取元素全部内容,希望文章能够帮你解决使用jQuery读取RSS源 – 无法读取元素所遇到的程序开发问题。
如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。
本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。