我有一个 ajax 函数可以从 RSS feed 加载图像,但得到 .parseHTML 不是函数错误
,因为我仍在使用 1.7.2。不幸的是,升级到 1.8 不是一个选择...
$.ajax({
type: "GET",
url: "url.com/feed",
dataType: "xml",
success: function(xml) {
var limit = 1;
$(xml).find('item').each(function(index) {
if (index < limit) {
var title = $(this).find('title').text();
var url = $(this).find('link').text();
var pubDate = $(this).find('pubDate').text();
var desc = $(this).find('description').text();
desc = $.parseHTML(desc);
var imgURL = desc[0].firstChild.srcset.split(',').pop().trim().split(' ')[0];
var alt = desc[0].firstChild.alt;
// Do something
return;
}
}); //end each
}
});
我使用 .praseHTML()
来定位图像的最后一个 srcset
属性以获取其 URL。这是我的 RSS 的样子
<item>
<title>Geoffrey Martinez Brews Up a Good Cup of Java for Compton</title>
<link>https://csudhnews.staging.wpengine.com/geoffrey-martinez/</link>
<pubDate>Wed, 08 Aug 2018 16:09:29 +0000</pubDate>
<dc:creator xmlns:dc='http://purl.org/dc/elements/1.1/'><![CDATA[Paul Browning]]></dc:creator>
<category><![CDATA[Features]]></category>
<category><![CDATA[Alumni]]></category>
<category><![CDATA[Business]]></category>
<category><![CDATA[Entrepreneurship]]></category>
<category><![CDATA[Latin American Studies]]></category>
<guid isPermaLink='false'>https://csudhnews.staging.wpengine.com/?p=32594</guid>
<description><![CDATA[<div><img width='300' height='168' src='https://csudhnews.staging.wpengine.com/wp-content/uploads/2018/08/Geoffrey-Martinez0006_FEATURE-1-300x168.jpg' class='attachment-medium size-medium wp-post-image' alt='Alumnus Geoffrey Martinez of Patria Coffee' style='margin-bottom: 15px;' srcset='https://csudhnews.staging.wpengine.com/wp-content/uploads/2018/08/Geoffrey-Martinez0006_FEATURE-1-300x168.jpg 300w, https://csudhnews.staging.wpengine.com/wp-content/uploads/2018/08/Geoffrey-Martinez0006_FEATURE-1-768x430.jpg 768w, https://csudhnews.staging.wpengine.com/wp-content/uploads/2018/08/Geoffrey-Martinez0006_FEATURE-1-1024x573.jpg 1024w, https://csudhnews.staging.wpengine.com/wp-content/uploads/2018/08/Geoffrey-Martinez0006_FEATURE-1-630x350.jpg 630w, https://csudhnews.staging.wpengine.com/wp-content/uploads/2018/08/Geoffrey-Martinez0006_FEATURE-1-750x420.jpg 750w, https://csudhnews.staging.wpengine.com/wp-content/uploads/2018/08/Geoffrey-Martinez0006_FEATURE-1-200x111.jpg 200w, https://csudhnews.staging.wpengine.com/wp-content/uploads/2018/08/Geoffrey-Martinez0006_FEATURE-1.jpg 1500w' sizes='(max-width: 300px) 100vw, 300px' /></div>It wasn’t the billowing smoke filling his apartment courtyard from a popcorn machine with a convention oven mounted on top that initially piqued Geoffrey Martinez’s curiosity, it was the smell of freshly roasting coffee beans that drew him in. “I had never seen coffee roasting before so it definitely caught my attention,” said the California […]]]></description>
</item>
最佳答案
这里确实不需要 jQuery。 Ajax 中的 X 首先代表 XML。
https://developer.mozilla.org/en-US/docs/Web/API/XMLHttpRequest/responseXML
使用responseXML属性获取文档,然后您可以在其上使用经典的DOM方法,例如element.querySelector。
关于javascript - jquery 1.7 的 .parseHTML() 替代品?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52213693/