我有一个ajax函数可以从RSS提要中加载图像,但是由于我仍在使用1.7.2,所以得到了一个.parseHTML is not a function error。不幸的是,不能升级到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/

10-11 02:40