gpt4 book ai didi

javascript - .parseHTML() 替代 jquery 1.7?

转载 作者:行者123 更新时间:2023-11-29 17:42:35 25 4
gpt4 key购买 nike

我有一个 ajax 函数可以从 RSS 提要加载图像,但是得到 .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 [&#8230;]]]></description>
</item>

最佳答案

在这里你真的不需要jQuery。 Ajax 中的 X 首先代表 XML。

https://developer.mozilla.org/en-US/docs/Web/API/XMLHttpRequest/responseXML

使用 responseXML 属性获取文档,然后您可以在其上使用经典的 DOM 方法,如 element.querySelector。

关于javascript - .parseHTML() 替代 jquery 1.7?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52213693/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com