问题描述
是否可以使用Node.js在服务器端使用jQuery选择器/ DOM操作?
Is it possible to use jQuery selectors/DOM manipulation on the server-side using Node.js?
推荐答案
更新(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
这篇关于我可以在Node.js中使用jQuery吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!