2017-09-20 14 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; 
関連する問題