(使用 CHEERIO)我想获取选项标签之间包含文本“9”的元素的值:
<option value="19437843939456"> 8.5 </option>
<option value="19437843972224"> 9 </option>
我想得到这个:19437843972224
,它是选项标签中value
属性的值
我如何通过 Cheerio 获得它?
最佳答案
Select the option by using
$("option")
Adding the
:contains('string')
to the selector will look for the text of the option.Finally the
val()
method will get the value of the option.
将它们组合起来以获得所需的结果:
$("option:contains('9')").val();
这是一个working example .
检查documentation了解更多信息。
更新
您需要使用正确的 select
元素,可以通过使用其 name
属性来实现:
$("select[name=id] option:contains('9')").val();
更复杂的example .
关于node.js - 如何根据cheerio中的文本获取元素的属性,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63540470/