javascript - jquery 1.7 的 .parseHTML() 替代品?

标签 javascript jquery ajax

我有一个 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 [&#8230;]]]></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/

相关文章:

jquery - 模糊后是否有任何选项可以将焦点重新集中在同一元素上

css - 文本不会换行

jquery - JQuery ajax调用的返回值

javascript - 在数组内的对象中提取数组

javascript - Spring mvc security and WEB-INF js,images access-拒绝从 'http://localhost:8081/xyz/static/internal/js/jquery-1.10.2.min.js'执行脚本

javascript - 从 xhttp.open ("Get"动态填充下拉列表;回复

jquery - 使用 jQuery 或 CSS 选择 jQueryUI 自动完成列表

javascript - 在 Chart.js 中隐藏 x 轴标签但显示工具提示

javascript - jQuery:按类名隐藏,然后按相同的类名显示下一个元素?

javascript - 尽管验证失败,仍通过 AJAX 提交表单