【问题标题】:How to get node line number in cheerio.js?如何在cheerio.js 中获取节点行号?
【发布时间】:2017-09-20 07:03:40
【问题描述】:

我正在使用cheerio.js,如下所示:

var $ = cheerio.load(html,{withStartIndices : true});

当我使用console.log($('#element1'));。它将返回带有字符位置的节点。

   { 
     type: 'tag',
     name: 'h6',
     attribs: { align: 'center', id: 'r' },
     children: [ [Object] ],
     next: null,
     startIndex: 310,
.......

有什么方法可以获取cheerio.js 中特定元素的行号?

【问题讨论】:

  • 你能告诉我它(withStartIndices)在哪个版本中工作,因为在最新版本中无法获取行号,因为没有允许 withStartIndices 的选项

标签: node.js npm cheerio


【解决方案1】:

这里有一个解决方案

const $ = cheerio.load(html, { withStartIndices: true });
const start = $('#element1').get(0).startIndex;
const lineNumber = html.substr(0, start).split('\n').length;

【讨论】:

  • 对我来说,我必须使用const $ = cheerio.load(html, { xmlMode: true, withStartIndices: true });
  • 你能告诉我它(withStartIndices)在哪个版本中工作,因为在最新版本中无法获取行号,因为没有允许 withStartIndices 的选项
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2011-06-22
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多