gpt4 book ai didi

javascript - jQuery 和 Node.js 实现简单的事情

转载 作者:太空宇宙 更新时间:2023-11-04 02:30:07 24 4
gpt4 key购买 nike

我使用以下命令安装了 jquery:npm install jquery 然后尝试运行:

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

我收到这个错误,我非常理解(node.js 中没有文档!):

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

在如此简单的情况下,如何让 jQuery 在没有 node.js 中的 document/window 的情况下工作?

<小时/>

PS:我尝试过: var $=require('jquery')(jsdom.jsdom().createWindow());,但不幸的是 npm install jsdom 给了我很多无法调试的 make 错误(这将是另一个问题,所以这里超出了主题)。

最佳答案

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

Fast, flexible, and lean implementation of core jQuery designed specifically for the server.

您的代码将如下所示:

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

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

关于javascript - jQuery 和 Node.js 实现简单的事情,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27482676/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com