nodeJS中的JSDOM:如何取回被操纵的html? [英] JSDOM in nodeJS: How do I get back the manipulated html?
本文介绍了nodeJS中的JSDOM:如何取回被操纵的html?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在尝试操纵远程HTML并将其操作返回。我决定使用JSDOM,但无法弄清楚如何获取被操纵的HTML。有什么想法?
I am trying to manipulate a remote HTML and return it manipulated. I decided to use JSDOM but cannot figure out how to get the manipulated HTML back. Any ideas?
jsdom.env({
url: "http://www.cnn.com",
scripts: ["http://code.jquery.com/jquery.js"],
done: function (err, window) {
var $ = window.$;
console.log("HN Links");
var src = $(".ghciTopStoryImage1 img").attr('src','http://lorempixel.com/396/220/');
var headline = $(".blkbigheader span").html('header');
var description = $(".blkbigheader").parent().find("p a:eq(0)").html('text');
// not working....
content =$(window.document).html();
}
});
推荐答案
var jsdom = require("jsdom").jsdom;
var serializeDocument = require("jsdom").serializeDocument;
var doc = jsdom("<!DOCTYPE html>hello");
serializeDocument(doc) === "<!DOCTYPE html><html><head></head><body>hello</body></html>";
doc.documentElement.outerHTML === "<html><head></head><body>hello</body></html>";
调整上面的例子就是 content = serializeDocument(window.document )
。
这篇关于nodeJS中的JSDOM:如何取回被操纵的html?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文