我可以在 Node.js 中使用 jQuery 吗? [英] Can I use jQuery with Node.js?
问题描述
是否可以使用 Node.js 在服务器端使用 jQuery 选择器/DOM 操作?
Is it possible to use jQuery selectors/DOM manipulation on the server-side using Node.js?
推荐答案
Update (27-Jun-18):jsdom
似乎有重大更新这导致原始答案不再有效.我发现 this 答案现在解释了如何使用 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);
注意:原始答案没有提到您还需要使用 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
<打击>然后:
require("jsdom").env("", function (err, window) {
if (err) {
console.error(err);
return;
}
var $ = require("jquery")(window);
});
这篇关于我可以在 Node.js 中使用 jQuery 吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!