Cheerio 互动版

在线工具推荐: Three.js AI纹理开发包 - YOLO合成数据生成器 - GLTF/GLB在线编辑 - 3D模型格式在线转换 - 可编程3D场景编辑器

hello world

为了更加直观的学习 cheerio ,我们开始 hello world

//模块引用
var cheerio = require('cheerio'),
    $ = cheerio.load('<h2 class="title">Hello world</h2>');

$('.title').text('Hello there!');
$('.title').addClass('Welcome');
//输出
console.log($.html());

输出

Hello there!

解析.load() 引入我们要解析的html。text() 设置 h2 中的文本。addClass() 给 h2 添加新的class。

在右侧代码框中第8行中输入 console.log($.html()); 查看我们解析之后的html。