2017-02-14 103 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