简体   繁体   English

带有node.js的jQuery用于简单的事情

[英]jQuery with node.js for simple things

I installed jquery with : npm install jquery and then tried to run: 我使用以下命令安装了jQuery: npm install jquery ,然后尝试运行:

var $ = require('jquery');
console.log($('<a href="">hkjhjkh</a>').text());

I get this error, that I understand very well (no document in node.js!) : 我得到这个错误,我理解得很好(node.js中没有文档!):

Error( "jQuery requires a window with a document" )

How to make jQuery work without a document / window in node.js in such a simple case? 在如此简单的情况下,如何使jQuery在node.js中没有document / window情况下工作?


PS: I tried with: var $=require('jquery')(jsdom.jsdom().createWindow()); PS:我尝试使用: var $=require('jquery')(jsdom.jsdom().createWindow()); , but unfortunately npm install jsdom gave me lots of make errors that I was unable to debug (that would be another question, so out of topic here). ,但是不幸的是npm install jsdom给了我很多我无法调试的制造错误(这是另一个问题,所以不在这里。)

Try cheerio ( https://github.com/cheeriojs/cheerio ): 尝试cheerio( https://github.com/cheeriojs/cheerio ):

Fast, flexible, and lean implementation of core jQuery designed specifically for the server. 专为服务器设计的核心jQuery的快速,灵活和精益实现。

Your code would look like this: 您的代码如下所示:

var cheerio = require('cheerio'),
    $ = cheerio.load('<a href="">hkjhjkh</a>');

console.log($('a').text());

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM