是否有可能在服务器端使用Node.js使用jQuery选择器/DOM操作?
当前回答
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
其他回答
2016年,事情就简单多了。使用控制台将jquery安装到node.js:
npm install jquery
将它绑定到node.js代码中的变量$(例如-我习惯了它):
var $ = require("jquery");
做的东西:
$.ajax({
url: 'gimme_json.php',
dataType: 'json',
method: 'GET',
data: { "now" : true }
});
也适用于gulp,因为它基于node.js。
据我所知没有。DOM是一个客户端的东西(jQuery不解析HTML,但解析DOM)。
下面是一些当前的Node.js项目:
https://github.com/ry/node/wiki (https://github.com/nodejs/node)
SimonW的djangode非常酷…
另一种选择是使用Underscore.js。它应该提供您可能需要的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
您必须使用新的JSDOM API来获取窗口。
const jsdom = require("jsdom");
const { window } = new jsdom.JSDOM(`...`);
var $ = require("jquery")(window);