是否有可能在服务器端使用Node.js使用jQuery选择器/DOM操作?
当前回答
这些解决方案在我的电子应用程序中没有一个对我有帮助。
我的解决方案(变通方案):
npm install jquery
在index.js文件中:
var jQuery = $ = require('jquery');
在你的.js文件中,以这样的方式编写jQuery函数:
jQuery(document).ready(function() {
其他回答
我的工作代码是:
npm install jquery
然后:
global.jQuery = require('jquery');
global.$ = global.jQuery;
或者如果窗口存在,则:
typeof window !== "undefined" ? window : this;
window.jQuery = require('jquery');
window.$ = window.jQuery;
jQuery模块可以通过以下方式安装:
npm install jquery
例子:
var $ = require('jquery');
var http = require('http');
var options = {
host: 'jquery.com',
port: 80,
path: '/'
};
var html = '';
http.get(options, function(res) {
res.on('data', function(data) {
// collect the data chunks to the variable named "html"
html += data;
}).on('end', function() {
// the whole of webpage data has been collected. parsing time!
var title = $(html).find('title').text();
console.log(title);
});
});
jQuery在Node.js中的引用**:
http://quaintous.com/2015/07/31/jqery-node-mystery/ http://www.hacksparrow.com/jquery-with-node-js.html
这些解决方案在我的电子应用程序中没有一个对我有帮助。
我的解决方案(变通方案):
npm install jquery
在index.js文件中:
var jQuery = $ = require('jquery');
在你的.js文件中,以这样的方式编写jQuery函数:
jQuery(document).ready(function() {
您必须使用新的JSDOM API来获取窗口。
const jsdom = require("jsdom");
const { window } = new jsdom.JSDOM(`...`);
var $ = require("jquery")(window);
是的,jQuery可以与Node.js一起使用。
步骤包括jQuery在节点项目:-
NPM jquery——保存 在代码中包含jquery
import jQuery from 'jquery';
const $ = jQuery;
我确实在node.js项目中一直使用jquery,特别是在chrome扩展的项目中。
例如,https://github.com/fxnoob/gesture-control-chrome-extension/blob/master/src/default_plugins/tab.js