gpt4 book ai didi

javascript - Jsdom 不返回文件

转载 作者:太空狗 更新时间:2023-10-29 16:39:32 26 4
gpt4 key购买 nike

我正在尝试使用 jsdom 加载本地 HTML 文件,这是代码

var config = {
file: "filename",
scripts: ["node_modules/jquery/dist/jquery.min.js"]
done: function(err, window){
console.log(window.document.URL)
console.log(window.document.children)
}
}
jsdom.env(config)

window.document.URL 显示正确的 url,但 window.document.innerHTML 没有任何数据。我什至尝试过

var config = {
text: *raw-html*,
scripts: ["node_modules/jquery/dist/jquery.min.js"]
done: function(err, window){
console.log(window.document.children)
}
}

但我得到了相同的结果,有什么帮助吗?

更新

我发现了

var rawhtml = fs.readFileSync("filename.html","utf8")
jsdom.env(
rawhtml,
["http://code.jquery.com/jquery.js"],
function (err, window) {
console.log(window.$("html").html())
}
);

按预期工作(它记录了 html 标签内的所有内容)

然后我发现

config = {
file: "filename.html",
scripts: ["http://code.jquery.com/jquery.js"],
done: function (err, window) {
var $ = window.$;
console.log($("html").html());
}
}
jsdom.env(config);

也可以,但同样不能正确创建document对象

“解决方案”

jsdom 不会以与浏览器控制台相同的方式显示 document.children 的输出,但是,您期望的所有功能都在那里。最终的工作 document 对象在下面

var func = function(document){
console.log(document.body.innerHTML)
document.body.innerHTML = "i changed the body"


}
function getdoc(file, func){
var document;
jsdom.env(
file,
(err, window) => {
document = window.document
func(document)
}
);
}
getdoc("index.html",code)

我使用 jsdom.env() 获取 document,然后将 document 传递给 func where所有有趣的 JavaScript 魔法都是免费发生的。请记住,对 文档 所做的任何更改都需要手动保存。

最佳答案

没有 document.innerHTML 属性。

检查文档中的“innerHTML”; - 你会得到 false

关于javascript - Jsdom 不返回文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37737490/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com