.eq(i)我未定义但有效?

时间:2019-01-12 01:24:53

标签: javascript jquery loops

一切正常,但是我试图理解原因。我正在从wordpress的RSS feed中解析项目,并将其添加到称为“新闻”的3个单独的列div中。这是我的代码

var urls = ['https://domain.xyz/feed1', 'https://domain.xyz/feed2','https://domain.xyz/feed3'];
$.each(urls, function(i,u){ // for each wordpress news feed url
    $.ajax(u,{
    type: "GET",
    dataType: "xml",
        success: function(xml) {
            var limit = 1;
            $(xml).find('item').each(function(index) { // for each item entry in each of the rss categories, show only 1 entry
                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(); // converts xml description to a string
                    var parser = new DOMParser();
                    var xmlDoc = parser.parseFromString(desc, "text/html"); // convert desc into document
                    var imgURL = xmlDoc.querySelector("img"); // target the image element
                    var alt = imgURL.alt;
                    // srcset not supported in IE so use src
                    // example xml item <img width="300" height="168" src="https://news.csudh.edu/wp-content/uploads/2018/12/Professor-Karen-Wilson-300x168.jpg">
                    // eq(i) traverses news elements and select i index
                    $(".news").eq(i).html('<a href="' + url + '" class="no-dec"><img class="newsImage" src="' + imgURL.src.replace("-300x168","") + '" title="' + title + '" alt="' + alt + '"></a><div class="news-text-area"><div class="newsHeadline"><a href="' + url + '" class="no-dec">'+ title +'</a></div><a href="' + url + '" class="read-more-link">READ MORE</a></div>');
                    return;
                }
            }); //end each
        }
    });
});

尽管控制台日志返回了$(".news").eq(i),但我仍坚持i is not defined.为何起作用,唯一使用i的地方是每个函数外部的参数,并且未在任何地方定义。

0 个答案:

没有答案