更新时间:2023-11-10 14:14:52
更新(27-Jun-18):看起来对 jsdom
进行了重大更新,导致原始答案不再有效。我找到了这个答案,解释了现在如何使用 jsdom
。我已复制下面的相关代码。
Update (27-Jun-18): It looks like there was a major update to jsdom
that causes the original answer to no longer work. I found this answer that explains how to use jsdom
now. I've copied the relevant code below.
var jsdom = require("jsdom");
const { JSDOM } = jsdom;
const { window } = new JSDOM();
const { document } = (new JSDOM('')).window;
global.document = document;
var $ = jQuery = require('jquery')(window);
注意:原始答案未提及您需要使用安装jsdom npm install jsdom
Note: The original answer fails to mention that it you will need to install jsdom as well using npm install jsdom
更新(2013年末):官方jQuery团队终于在npm上接管了 jquery
包的管理:
Update (late 2013): The official jQuery team finally took over the management of the jquery
package on npm:
npm install jquery
然后:
Then:
require("jsdom").env("", function (err, window) {
if (err) {
console.error(err);
return;
}
var $ = require("jquery")(window);
});