Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
225 views
in Technique[技术] by (71.8m points)

javascript - 我可以将jQuery与Node.js一起使用吗?(Can I use jQuery with Node.js?)

是否可以使用Node.js在服务器端使用jQuery选择器/ DOM操作?

  ask by John translate from so

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

Update (27-Jun-18) : It looks like there was a major update to jsdom that causes the original answer to no longer work.

(更新(18-Jun-18) :似乎对jsdom进行了重大更新,导致原来的答案不再起作用。)

I found this answer that explains how to use jsdom now.

(我找到了这个答案,它解释了现在如何使用jsdom 。)

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);

Note: The original answer fails to mention that it you will need to install jsdom as well using npm install jsdom

(注意:原始答案没有提及您需要使用npm install jsdom)

Update (late 2013) : The official jQuery team finally took over the management of the jquery package on npm:

(更新(2013年末) :官方jQuery团队最终在npm上接管了jquery软件包的管理:)

npm install jquery

Then:

( 然后: )

 
 
 
  
  require("jsdom").env("", function (err, window) { if (err) { console.error(err); return; } var $ = require("jquery")(window); });
 
  


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...