2017-09-20 2 views
2

J'utilise cheerio.js comme ci-dessous:Comment obtenir le numéro de ligne de noeud dans cheerio.js?

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

Lorsque j'utilise console.log($('#element1'));. Il renverra le noeud avec la position de caractère.

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

Est-il possible d'obtenir le numéro de ligne dans cheerio.js pour un élément spécifique?

Répondre

0

est ici une solution

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