9

如何document从 html 源创建对象并使用node.js 中的document.*函数?getElementById

4

2 回答 2

11

您可能想要 DOM 的jsdom之类的 javascript 实现。

于 2010-08-18T15:57:10.943 回答
4

如果您只想使用类似 jQuery 的 API 来遍历和处理 HTML 标记,更好的选择是cheerio

jsdom 是一个成熟的 DOM 实现,甚至可以运行页面自带的 JS。结果,它很重。如果您不需要任何这些功能,cheerio 的速度要快 8 倍。

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

$('h2.title').text('Hello there!');
$('h2').addClass('welcome');

$.html();
//=> <h2 class="title welcome">Hello there!</h2>
于 2013-07-14T17:15:53.047 回答