Select 个具有 cheerio 属性的元素

Select elements with an attribute with cheerio

select 具有特定属性的所有 dom 元素的最有效方法是什么。

<input name="mode">

对于纯 javascript 我会使用:document.querySelectorAll("[name='mode']") 或者 document.querySelectorAll("[name]") 如果我不关心属性值。

好的,我在 cheerio 文档中找到了它,这是您的操作方法:

$('[name=mode]')

cheerio docs: Selectors

出于某种原因,接受的答案对我不起作用(在此处使用 cheerio ^1.0.0-rc.2)。

但对于以下标记:

<input value="123" name="data[text_amount]">

这确实有效:

$('input[name="data[text_amount]"]'));

双引号起到了神奇的作用。从 cheerio's help docs.

得到的