2017-09-20 36 views
2

我使用cheerio.js按如下:如何获取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中某一特定元素的行号?

回答

0

这里有一个解决方案

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