这是片段
let thtml = "<html><body><div id=app><div id=viewer></div></div></body></html>"
jsdom.env({
html: thtml ,
done: function(err: any, window: any) {
console.log(window.document.getElementById("viewer"));
}
});
这里
window.document.getElementById("viewer")
返回一个空的 HTMLDivElement
。
在 Chrome 中,我得到了 div id="viewer"
,期望它应该返回相同的 div 元素。
这是一个错误还是我在这里遗漏了一些东西?
jsdom 返回的元素设计为空:https://github.com/jsdom/jsdom/issues/1891#issuecomment-345889214
您应该做的是使用 DOM API 来操作返回的对象。