2017-09-20 52 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; 
相關問題