【问题标题】:HTMLElement.querySelector() using cheerioHTMLElement.querySelector() 使用cheerio
【发布时间】:2021-03-03 12:14:13
【问题描述】:

使用原生 JavaScript,你可以编写

const box = document.querySelector(".box");
const button = box.querySelector(".button");

Cheerio 怎么样? 我想做类似的事情:

const box = $(".box");
const button = box.$(".button")

我问是因为我必须遍历元素然后在该元素中找到一个锚标记:

const $ = cheerio.load(stdout)
      const boxes = $(".box").map((i, element) => element)
      boxes.each((i, element) => ?? maybe element.querySelector("a") )

【问题讨论】:

    标签: javascript cheerio


    【解决方案1】:

    如果你不想使用descendant combinator,你可以使用find

    const button = box.find("button");
    

    【讨论】:

    • 我遇到一个错误,说“TypeError: element.find is not a function”
    • 我的代码:boxes.each((i, element) => urls.push(element.find("a").attr('href')))
    • 试试 $(element).find("a")
    猜你喜欢
    • 1970-01-01
    • 2020-09-27
    • 1970-01-01
    • 2023-04-01
    • 2014-08-10
    • 1970-01-01
    • 2021-06-17
    • 2020-07-15
    • 2021-05-27
    相关资源
    最近更新 更多