HTMLElement.querySelector() 使用cheerio

问题描述

使用 vanilla 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") )

解决方法

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

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