2017-02-14 5 views
1

В Cheerio, как вы получаете только текст текущего узла?Получить только текст текущего узла

var cheerio = require('cheerio') 

const htmlString = '<div>hello<span>world</span></div>' 

$ = cheerio.load(htmlString, { ignoreWhitespace: true }) 

console.log($('div').text()) //helloworld 
console.log($('span').text()) //world 

Как вы можете получить только hello?

ответ

0

Вы можете сделать это:

console.log($('div').contents().first().text()) # hello 
+0

См https://github.com/cheeriojs/cheerio/issues/146 для справки – Kevin

Смежные вопросы