【问题标题】:Cheerio Get Image SrcCheerio 获取图像源
【发布时间】:2020-07-17 10:43:52
【问题描述】:

我尝试为一个表创建图像并将该链接推送到数组中。当我运行代码img 时得到一个未定义的。 我该如何解决这个问题。

$("body > form > table:nth-child(5) > tbody > tr").each((index, element) => {
                if (index === 0) return true;

                const tds = $(element).find("td");

                const img = $(tds[0]).attr('src'); // undefined
                const flight = $(tds[1]).text(); //working
                const origin = $(tds[2]).text(); //working
                const time = $(tds[3]).text(); //working
                const estimted = $(tds[4]).text(); //working
                const status = $(tds[5]).text(); //working

                console.log(img);

                const tableRow = {
                    flight,
                    origin,
                    time,
                    estimted,
                    status
                };
            });

【问题讨论】:

  • 尝试 $(tds[0]).find('img').attr('src') 而不是 $(tds[0]).attr('src')
  • 您应该向我们提供html代码

标签: javascript jquery node.js cheerio


【解决方案1】:

$(tds[0]).attr('src') 将未定义,因为 td 不是图像。

也许你想要:$(tds[0]).find('img').attr('src')

【讨论】:

    猜你喜欢
    • 2018-05-12
    • 1970-01-01
    • 2020-07-15
    • 2020-12-13
    • 1970-01-01
    • 1970-01-01
    • 2015-10-13
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多