2017-09-20 6 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;