簡體   English   中英

Jsdom沒有歸還文件

[英]Jsdom not returning document

我正在嘗試使用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 ,其中所有有趣的javascript魔法都可以自由發生。 請記住,需要手動保存對document所做的任何更改。

沒有document.innerHTML屬性。

檢查"innerHTML" in document ; 你會得到false

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM