jsdom 4.0 發布,該版本基於全新的 vm 模塊,避免上下文原生模塊依賴。使用上更加簡單,無需 C++ 工具鏈進行編譯。改進介紹請看這裡。
jsdom 是 WHATWG DOM 和 HTML 標準的 JavaScript 實現,主要用於 io.js 框架。
示例代碼:
// Count all of the links from the io.js build pagevar jsdom = require("jsdom");jsdom.env( "https://iojs.org/dist/", ["http://code.jquery.com/jquery.js"], function (errors, window) { console.log("there have been", window.$("a").length - 4, "io.js releases!"); });
或者:
// Run some jQuery on a html fragmentvar jsdom = require("jsdom");jsdom.env( '<p><a class="the-link" href="https://github.com/tmpvar/jsdom">jsdom!</a></p>', ["http://code.jquery.com/jquery.js"], function (errors, window) { console.log("contents of a.the-link:", window.$("a.the-link").text()); });