Javascript 将第一个img src从解析的提要传递到另一个元素';s bg img属性

Javascript 将第一个img src从解析的提要传递到另一个元素';s bg img属性,javascript,jquery,Javascript,Jquery,所以,本质上我是在用jQuery解析一些RSS提要。我想获取最新解析的图像并获取最新的(或第一个实例)img src,然后用它的内容填充第二个指定元素的css背景图像:属性。因此,来自feed输出的第一个实例imgsrc,也将用它的内容填充第二个元素的背景图像。我一直在使用以下逻辑。但是,我总是在控制台中得到背景图像:“未定义” 由于某些原因,它没有从下面第一行读取或捕获填充图像的第一个实例 下面是我的上下文的完整JS 我还尝试在rss提要JS之前和之后移动上述两行 jQuery(documen

所以,本质上我是在用jQuery解析一些RSS提要。我想获取最新解析的图像并获取最新的(或第一个实例)
img src
,然后用它的内容填充第二个指定元素的css
背景图像:
属性。因此,来自feed输出的第一个实例
imgsrc
,也将用它的内容填充第二个元素的背景图像。我一直在使用以下逻辑。但是,我总是在控制台中得到
背景图像:“未定义”

由于某些原因,它没有从下面第一行读取或捕获填充图像的第一个实例

下面是我的上下文的完整JS

我还尝试在rss提要JS之前和之后移动上述两行

jQuery(document).ready(函数($){
var url=window.location.href;
如果(url.indexOf('check')>-1){
$(“.feed1”).rss(”http://www.thehollywoodgossip.com/rss.xml",
{
限额:17,
entryTemplate:“
{teaserImage}{shortBodyPlain}” }) $(“.feed2”).rss(”http://www.thehollywoodgossip.com/rss.xml", { 限额:17, entryTemplate:“
{teaserImage}{shortBodyPlain}” }) $(“.feed3”).rss(”http://www.thehollywoodgossip.com/rss.xml", { 限额:17, entryTemplate:“
{teaserImage}{shortBodyPlain}” }) } 如果(url.indexOf('movies')>-1){ $(“.feed1”).rss(”http://www.thehollywoodgossip.com/rss.xml", { 限额:17, entryTemplate:“
{teaserImage}{shortBodyPlain}” }) $(“.feed2”).rss(”http://www.thehollywoodgossip.com/rss.xml", { 限额:17, entryTemplate:“
{teaserImage}{shortBodyPlain}” }) $(“.feed3”).rss(”http://www.thehollywoodgossip.com/rss.xml", { 限额:17, entryTemplate:“
{teaserImage}{shortBodyPlain}” }) } 如果(url.indexOf('host')>-1){ $(“.feed1”).rss(”http://www.dlisted.com/rss.xml", { 限额:17, entryTemplate:“
{teaserImage}{shortBodyPlain}” }) $(“.feed2”).rss(”http://stupidcelebrities.net/feed/", { 限额:17, entryTemplate:“
{teaserImage}{shortBodyPlain}” }) $(“.feed3”).rss(”http://www.thehollywoodgossip.com/rss.xml", { 限额:17, entryTemplate:“
{teaserImage}{shortBodyPlain}” }) } 如果(url.indexOf('TheCollegeLife')>-1){ $(“.feed1”).rss(”http://www.thehollywoodgossip.com/rss.xml", { 限额:17, entryTemplate:“
{teaserImage}{shortBodyPlain}” }) $(“.feed2”).rss(”http://www.thehollywoodgossip.com/rss.xml", { 限额:17, entryTemplate:“
{teaserImage}{shortBodyPlain}” }) $(“.feed3”).rss(”http://www.thehollywoodgossip.com/rss.xml", { 限额:17, entryTemplate:“
{teaserImage}{shortBodyPlain}” }) } 如果(url.indexOf('WingmanConnect')>-1){ $(“.feed1”).rss(”http://www.thehollywoodgossip.com/rss.xml", { 限额:17, entryTemplate:“
{teaserImage}{shortBodyPlain}” }) $(“.feed2”).rss(”http://www.thehollywoodgossip.com/rss.xml", { 限额:17, entryTemplate:“
{teaserImage}{shortBodyPlain}” }) $(“.feed3”).rss(”http://www.thehollywoodgossip.com/rss.xml", { 限额:17, entryTemplate:“
{teaserImage}{shortBodyPlain}” }) } 如果(url.indexOf('Media2point0')>-1){ $(“.feed1”).rss(”http://www.thehollywoodgossip.com/rss.xml", { 限额:17, entryTemplate:“
{teaserImage}{shortBodyPlain}” }) $(“.feed2”).rss(”http://www.thehollywoodgossip.com/rss.xml", { 限额:17, entryTemplate:“
{teaserImage}{shortBodyPlain}” }) $(“.feed3”).rss(”http://www.thehollywoodgossip.com/rss.xml", { 限额:17, entryTemplate:“
{teaserImage}{shortBodyPlain}” }) } 如果(url.indexOf('Brokepoint')>-1){ $(“.feed1”).rss(”http://www.thehollywoodgossip.com/rss.xml", { 限额:17, entryTemplate:“
{teaserImage}{shortBodyPlain}” }) $(“.feed2”).rss(”http://www.thehollywoodgossip.com/rss.xml", { 限额:17, entryTemplate:“
{teaserImage}{shortBodyPlain}” }) $(“.feed3”).rss(”http://www.thehollywoodgossip.com/rss.xml", { 限额:17, entryTemplate:“
{teaserImage}{shortBodyPlain}” }) } var HeadImg=$('.featimg-img').first().attr('src'); $('.site-featured content').css('background-image','url('+HeadImg+')); })

注意://是,
{teaserImage}
按建议呈现为

function waitForCompleteRender()
{
    var HeadImg = $('.featimg img').first();
    if (HeadImg.length == 0) return setTimeout(waitForCompleteRender, 200);

    $('.site-featured-content').css('background-image', 'url(' + HeadImg.attr('src') + ')');

}

waitForCompleteRender();
编辑

请注意,这种方法对于生产使用来说是非常危险的(如果根本不渲染图像)。您可能希望添加一条if语句,该语句将处理X次之后的错误,但没有成功…

建议:

function waitForCompleteRender()
{
    var HeadImg = $('.featimg img').first();
    if (HeadImg.length == 0) return setTimeout(waitForCompleteRender, 200);

    $('.site-featured-content').css('background-image', 'url(' + HeadImg.attr('src') + ')');

}

waitForCompleteRender();
编辑


请注意,这种方法对于生产使用来说是非常危险的(如果根本不渲染图像)。您可能需要添加一个if语句,该语句将在X次之后处理错误,但没有成功….

您似乎在使用某种模板系统。因此,您可能试图在将
img
注入html之前访问它。我建议使用超时。您可以测试它是否在调试模式下工作?您似乎正在使用一些模板系统。因此,您可能试图在将
img
注入html之前访问它。我建议使用超时。您可以测试它是否在调试模式下工作?
function waitForCompleteRender()
{
    var HeadImg = $('.featimg img').first();
    if (HeadImg.length == 0) return setTimeout(waitForCompleteRender, 200);

    $('.site-featured-content').css('background-image', 'url(' + HeadImg.attr('src') + ')');

}

waitForCompleteRender();