javascript - 操作 jQuery 得到错误:“jQuery 需要一个窗口和一个文档”
问题描述
如果有人浏览了 jQuery 的未压缩版本,可能会遇到如下情况:
if ( typeof module === "object" && typeof module.exports === "object" ) {
// For CommonJS and CommonJS-like environments where a proper `window`
// is present, execute the factory and get jQuery.
// For environments that do not have a `window` with a `document`
// (such as Node.js), expose a factory as module.exports.
// This accentuates the need for the creation of a real `window`.
// e.g. var jQuery = require("jquery")(window);
// See ticket #14549 for more info.
module.exports = global.document ?
factory( global, true ) :
function( w ) {
if ( !w.document ) {
throw new Error( "jQuery requires a window with a document" );
}
return factory( w );
};
} else {
factory( global );
}
注意throw new Error()
部分。如果我没记错的话,它只会在不存在窗口和文档的情况下引发错误。我知道像 Node.js 这样的东西可能会触发这个错误,但是普通的 HTML、CSS 和 JS 网页可以以任何方式触发这个错误吗?
解决方案
Node.js 不会在这里抛出错误,因为它包含一个内部带有“文档”的全局对象;您可以将 jquery 与 Node.js 一起使用。
最好使用CherrioJS,它专门设计为“类似于 jQuery,但用于服务器。
尝试将 jQuery 与“ ElectronJS ”或“ Headless Chrome ”一起使用是会引发此错误的示例。
推荐阅读
- acumatica - 如何使用 Acumatica API 发送电子邮件?
- wordpress - 通过 REST API 设置 WooCommerce 产品密码
- javascript - Javascript - 这两个对象之间有区别吗?
- php - php stdobject - 调用默认函数
- javascript - hasOwnProperty not working in array of objects, combining objects
- r - 如何将日期和时间转换为R中的数值
- ruby-on-rails - Rails 5 - 重定向时保留 :js 格式
- mysql - 如何将 sql server 连接到 app.js 文件?
- c# - 如何使用 Entity Framework .SqlQuery 以便它可以同时被多个客户端使用?
- google-cloud-platform - 在本地运行 ESP 时出现 Cloud Endpoints 错误