只获取当前节点的文本

Get the text of the current node only

在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

你可以这样做:

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