Jquery Cheerio js html

Jquery Cheerio js html,jquery,html,cheerio,Jquery,Html,Cheerio,我正在尝试用cheerio解析html字符串 我遇到的问题是获取表列的索引 由于ChereIO选择器看起来像我尝试的jQuery: $('td:contains("Name")').index(); 但它可以与jQuery一起使用,但与cheerio不能一起使用 你们有什么想法吗 编辑:正如您所问,这里有一个html示例,它是一个简单的表,但列数可以更改 <table> <tr> <td>ID</td> <td&g

我正在尝试用cheerio解析html字符串 我遇到的问题是获取表列的索引

由于ChereIO选择器看起来像我尝试的jQuery:

$('td:contains("Name")').index();
但它可以与jQuery一起使用,但与cheerio不能一起使用

你们有什么想法吗

编辑:正如您所问,这里有一个html示例,它是一个简单的表,但列数可以更改

<table>
   <tr>
     <td>ID</td>
     <td>Name</td>
     <td>Age</td>
   </tr>
   <tr>
      ...
   </tr>
</table>

身份证件
名称
年龄
...

我认为Cheerio没有实现该选择器。它类似于jQuery,但只是jQuery完整实现的一个子集

你可以这样做来解决这个问题:

var cheerio = require('cheerio'),
    $ = cheerio.load('<table><tr><td>ID</td><td>Name</td><td>Age</td></tr></table>');

var nameIndex = $('td').map(function(i, e) {
  if ($(this).text() === 'Name')
    return i;
})[0];

// Outputs "1" in this example.
console.log(nameIndex);
var cheerio=require('cheerio'),
$=cheerio.load('IDNameAge');
var nameIndex=$('td').map(函数(i,e){
if($(this).text()=='Name')
返回i;
})[0];
//在本例中输出“1”。
console.log(nameIndex);

github上的Cheerio版本(0.17.0版)实际上支持
.index()
。据我所知,npm中的版本实际上有相同的版本号,但没有


因此,如果您想使用
索引
,您需要从github而不是通过npm将其下拉。

您可以向我们展示表格的html吗?是的,似乎是这样。index()没有在cheerio中实现。谢谢@DaveWard,它帮助了我!