Fre*_* J. 6 html meteor cheerio
此 Meteor 服务器代码尝试使用包从 html 字符串中提取innerHTML cheerio,但错误表明elements没有方法“size”
我做错了什么以及如何解决?谢谢
这是 html;
<span class='errorMsg'>some text </span>
message: (html, typeMsg) => {
let $ = cheerio.load(html);
const selection = 'span.' + typeMsg;
const elements = $(selection);
return elements.size() > 0 ? elements.get(0).innerHTML.trim() : '';
}
Run Code Online (Sandbox Code Playgroud)
var cheerio = require('cheerio');
var $ = cheerio.load('<b>hello</b> world');
// innerHTML
$('body').html()
// <b>hello</b> world
// outerHTML
$.html($('body'))
// <body><b>hello</b> world</body>
// innerText
$('body').text()
// hello world
Run Code Online (Sandbox Code Playgroud)
文档: https: //api.jquery.com/html/#html1
经过几次尝试和错误并尝试理解文档后,哪种云可以从更多解释中受益。
选项1
const element = $(selection).eq(0);
return element ? element.text().trim() : '';
Run Code Online (Sandbox Code Playgroud)
选项2
const element = $(selection).get([0]);
return element ? element.children[0].data.trim() : '';
Run Code Online (Sandbox Code Playgroud)
在本例中我使用了选项 1。
| 归档时间: |
|
| 查看次数: |
12928 次 |
| 最近记录: |